change for loop to while loop

still problems to fix