More cleanup and memory leak fixes