Implemented main Algo