fixed bug catching leastsq errors