Moved logic into while loop.