fixes to memory management
added memory test