First version of algorithm