Fixed multithreading bugs in Vipul's code.