Fully working implementation with basic (but slow) tests.