Fix thread unsafe bug