Remove unused variables and fix some possible memory leaks.