Improved code, should not show glitches