First working version of algorithm