implement serial and multithread version