Copied file-handling system from old library.

Also updated slightly in the process.