Refactor, hopefully making it easier to go to a file or stdout
as desired.