Fixed issues with thread safety