Improve handling of unexpected errors