Changed variable i in while loop