implemented main algo