made optimal piecewise faster