added error handling function