Code refactored to be more modular.  Also testing output directly
against cal output, rather than against copied-and-pasted output.