Tightened up tests and added some memory management tweaks