Fixes to error handling