You've found your error but, IMO, "Illegal modulus zero . . ." shouldn't be an error. A number mod zero is perfectly well-defined.
The trick is that '%' doesn't need to be defined in terms of division. It's better defined as,
$z == $x % $y
if $z is a solution of $z == $x - $n * $y for some integer $n
If $y is zero, $x % 0 == $x. If $y is one, $z is the fractional part of $x.
This definition is good for any additive group, since multiplication by an integer has a natural definition there even if multiplication of group elements doesn't exist. That would permit useful overloading of '%' for vectors or other objects with no useful group multiplication.