modified to thrading funcion