Use while loop instead of for