Fix a bunch of memory leaks. There are probably still some, but we can't all be perfect.