cleaned optimization code