Move as much of this as you can to a separate file please to make the merge with the postprocess branch less of a nightmare.