lots of memory leak fixes