Fix crash and leak