added new threshold code using constant midpoint