Fixed most of the memory leaks.