Added some of the main algorithm