outsourced algorithm, added documentation