Changed algoritm