Tidy up and fixed loop processing