seems that now it works correctly, although still not equal to optimal value