Tidied up example code. Added more unit tests.