cleanup and changes for fixing legend issues

Changed the code for writing to the PNG files for plot3 and plot4
instead of copying it.  also made some minor changes to clean up the
shared code