Improvements to error handling