refactoring and pep8 formatting