Refactor a lot of post code into more testable form.