change for loop to while loop