more loop refactoring