Tidy up the code

Tidy up the code by removing duplicated and unused imports, adding
missing imports and adhering to Python coding standards.