Final version, all tests pass