Refactor to split out CountingIterator.

We also move the construction functions into a separate class, for ease
of access.