Added shutdown function for error handling