Add minor refactorings and pep8 fixes