Reduced write-to-file functions; Cleaned up a bit