Remove some duplicate imports and (mostly) tidy up to PEP8.

Lots of minor changes. Also there were several long lines and unneeded vertical space that's now removed.

There were also a few sections containing tabs. I **think** all looks well, but would appreciate someone else quickly eyeballing it to make sure I've not wrecked any indentation.