Added a small degree of thread safety.