adding code to write to files and other fixes