rewrite of the mutlithreading code