Changed the for loop to a while loop.