This basically combines @timmccool's detector with an encoder so that it works even after the green flag.