initial single-threaded algoritm