More memory management fixes