Made thread-safe with ReentrantLock