added code to write the plots to files