Latest version of code swith plotting functions and write to file