i just remembered an old post i made on another forum that would be quite relevant here:
http://forum.swiftirc.net/viewtopic.php?f=32&t=4003
both of the issues i mentioned in my post (about modulo and the effect of $replace() when used with certain operators) are present in this snippet.