Refactor iterators to use for_each