modified for mtultithreading