refactor out for loop