start main algorithm