changed for to while loop