Added some thread safety.