So apparently there is this bug in javascript..
Modulo values don't work properly with negative numbers:
-2%10 equals -2
when it should equal 8
A simple work around is described on this site
So instead of -5%4 which gives the wrong answer in JavaScript we substitute ((-5%4)+4)%4
Not really a big deal but thought I should bring this to attention.. It'd be useful to implement if it's not too much hassle.