Fixed a couple of memory leaks and some other things.