optimization now uses gradient