Fixed repeat helper

* Added comprehensive tests
* Ensured memory use doesn't balloon for high indexes
Merge pull request #2182 from jroper/repeat-fix

Fixed repeat helper