Expose the close() method and implement AutoCloseable

Also some minor cleanups for unused things.