TST refactor and test for thread safety