Fixes for crash on exit