Fixed possible crash on exit