Memory management fixes