Change while loop to for loop