added explanation on the algorithm