Finally fix the memory bugs