refactoring loops