is this algorithm correct?