commented algorithm