Revised the error handling