Replace costly modulo operation with 2 ifs and addition/substraction