remade the memory management