minor loop refactoring