finshed multi-thread version