batov's algo