main algorithm simplified