fixed bug with potentially changing global variable in thread