Add tests for command line arguments.  Refactor a bit.

Just a start at adding tests so that refactoring is easier and safer.