changed a for loop to a while loop