Fix variable typo and double-free error