[Modify] Using for loop instead of while loop