Fixed a whole bunch of memory leaks.