multithreading (not stable for wrong inputs)