Added multithreading with lock for safe data sharing