An initial working version of the algorithm