Fixed repeat helper

* Added comprehensive tests
* Ensured memory use doesn't balloon for high indexes

Merge to 2.2.x required.