Changed the while loop to a for loop