Improved the error handling