Fix problems with memory leaks