Fix pathological recursion; add deallocation calls