Optimize
Finish the logic of optimize.