nit: this algorithm could use a comment