Refactor out functions: move common functionality to _set_output.