algoritm works