completed algoritm