Main program of the algorithm