Added lock for thread synchronization