Fixed a lot of memory management problems.