Added error checking to writing config files, and a function for writing directly to a given
filename.