Fix the memory leak in a cleaner fashion