Refactor writing output.