a few fixes to memory management