Fix modulo arithmetic

Tighten up code