Probably not the optimal solution, but tests pass.