Refactor for testing

Re-arrange some bits to make testing work