Refactoring so that the repository changesets are looped through as few times as possible.