added support for writing to output file