Fix the memory allocation/freeing