Fixed thread-safety problem