Added thread synchronization