optimize performance, add thread safety