improve glitch algorithm