refactor loops