The optimization function.