Generalize the algorithm a bit