Tidies things up a little. Adds some tests.