better handling of error in CG
better handling of error in CG