My solution to writing the loop