Fixed a problem where the loop flag would be removed prematurely.