converting for in range to a while, to make code more understandable