fixed the error handling function