memory management improvements in several files