Fixed thread safety issue