Switch to argparse for command line argument processing. Consolidated some of the __main__ code into a couple of functions.