better wiring algorithm