Some minor improvements to error handling