refactor and additional cleanup with some PEP8 exceptions for line length in the interest of practicality.