Lots of memory leak fixes