Fixed delete function and a memleak