Fix loop end bug (thanks CyberBotX)