replaced a few for loops with iterators