PEP8 fixups and code refactorization

Following multiple PEP8 hints to make code look nice.