DW: Code refactor and pep8 fixes