some refactoring wrt thread safety