Refactor out loop