Fixed some segfaults that occur on exit.