Refactor read function, add main function

* read() now handles an empty or nonexistent output file.
* main() allows the module to be imported.