Added error handling function