Modified for Levenberg-Marquardt opt.