main algorithm was implemented