Fixed thread synchronization bug