First fully functional version. Passes all tests.