add messily coded file-saving function