Refactoring using iterators