Use for loop rather than while loop