Use for loop instead of while loop