[RFC]Eleminate memory leaks

I am doing more tests on it. Comments are welcome.