Need to be careful with back edge detection - forward edge can screw up the algorithm