starting cleanup, lint and flake

Here is my first patch,
running pylint, pyflake starting to clean up the code, remove duplicates, remove uneeded global, 
moving top level code into main routine, removing lamda for list comprehension, adding docs, etc etc.