File handling

This isn't quite ready to merge, but I'm opening the pull request to make it easier to track progress.