fix glitches in the algorithm