fix locking issue due to incorrect use of threads