Fix possible crash on exit