Cleaned up the IO and file processing. Note that I have pulled out some of the functionality so I
can integrate it more cleanly later.