First version of new algorithm