some code clean up and memory leak fixes