Added functionality to save to file upon programme termination