Patch files after setting user directories.

Also, externalize some file-related functions.