tidied up some loop statements