made some loop logic easier to follow