Working, compilable version that uses the Clone trait for the purpose
of explaining the correctness of the algorithm.