Refactored to prepare thread safety.