Unify handling of writes to stdout, and other cleanup.