fixed multi-step memory freeing