Changed a for loop to a do while loop