autopep8 and minor refactoring