adding threadlocal code, didn't quite understand