Cleaning up iterators, removing duplication and bug fixes