Refactor loop