Fix thread-unsafe bugs relating to two functions.