memory management fixes; need testing