Improve code health

- Change string formatting to arguments in logging commands as this is how
  they should be called.
- Tidy some whitespace.
- Sort out some ambiguous indentation.
- Remove unused import.
- Compact some lone lines.
- Correct 'not x in y' to 'x not in y'.