refactor for loop