pulling out common code
updated return codes for bfgs optimization