Initial solution (still a few TODOs, but all tests pass