speed up by replacing a mod with a subtraction