refactoring loop