Completely re-written to use a 'dataset' object rather than a dozen global variables.