general sketch of the algorithm