tightened up the while loop