Changed for loop to while loop. Broke if-else statement and needs to be
fixed.