Refactor plot generation into functions, and write PlotToPng() function that takes generated plot and outputs to png device