Improved rewiring logic