Fixing thread logic