Committed the wrong version by accident. Starting again.
First working version. All tests pass.