made some fixes based on Cris's feedback. Still does not 100% catch all errors.