Fix deprecation warning.

Fix deprecation warning by simply propagating the IOException that gets thrown
by close().
Fix unused variable warning.

Rename a few variables so it's clearer what they are.
Fix formatting.
Extract a bunch of inner classes.