Big rewrite bringing static polymorphism and a generally slightly
better design.
Add lots of tests.