Created main algorithm