This seems like a generic function that also other algorithms could use?