Performance improvements to avoid using __iter__

Fixes for #28.

Contains reasonably uncontroversial changes which reasonably drastically improve the
performance of the tests.