Free memory on close
Proper cleanup and typo fixes