Initial version of the algorithm