refactor to support command line

Sorry for the messy patch. Lots of little things: command line interface, spaces instead  of tabs, mostly pep8 compliant, etc.