refactoring for thread-safe