Refactoring each loops