Fix some of the memory leaks but it is fundamentally broken.