Various code cleanups

Fixes most of errors found by PyFlakes, mostly unused variables or
imports.