better solution to kata using a loop
tidy up and add comments