fix problems with mod and negative numbers