Optimize by replacing modulo with bitemask - fix failing test case