Memory Leak fixes

Some memory leak fixes and code clean up.