refactoring for loops