first round of refactoring

separated out key portions of code into functions and made everything
pep8 compliant
Merge pull request #1 from esonderegger/master

first round of refactoring