Fixed logic errors involving modulus