changed to while loop