Enhanced the error handling