improved the search for gates

the gate now selects the nearest gate top block to be toggled