Done some rewriting and made the tests green again.
Still some tidying up and more tests to write.