more cleanup and memory leak fixes