some memory management fixes