Improved optimization function, defined referring epxressions and feedback.