Fixing some thread synchronization issues.