Fixing memory deallocation issues