refactor loop