cleanup of bug fixes. some pep8 corrections and refactoring