Bug fixes in error handling