add support for input and output files.
refactor to be a little more dry