Pre-big changes to main and functions
Minor changes in functions. All figure and table output code was exported.