ML: better regularized cost function and gradient solution