Reworking loops to stop overwrite

Signed-off-by: Richard Walker <richard@monkeybruiser.co.uk>