Added more robust error handling and auto-restart on critical error.