Replaced while loop with for, for loop for for each