modify main algorithm