Fix multiple thread bugs.