Refactor common code and fix for PEP8 1.4.6.