Refactoring: loop replace