Basic grammar fixes
Widespread refactoring:
- Reduce duplicated code
- Use argparse instead of sys.argv directly
- Add 'quiet' option for use from cron
- Move base-level inline code to functions to enable the above

Code still needs a lot of work, output should still be identical to the
original version.