Refactor for loop