loop refactor