Some memory management bug corrections