main algorithm is working