fixed the loop issue and some dodgy formatting