Mainly PEP8 stuff.

Tidied up the code a bit. Also fixed error where a non-existent variable was being referenced.