Fixed repeat helper

* Added comprehensive tests
* Ensured memory use doesn't balloon for high indexes
[#1449] Handle misformated cookies