Code clean up.

Assorted clean up and optimizations not completed in initial script
due to time constraints.
Added optparse.

Added command line arguments for specifying inputs.