Reverted back to my function for finding the nearest gate.

If `r = -1`, `nearest` would become 1 and the search radius is then 1.
Checking if `r < 0` was faulty because `r` was never changed (So if it was -1, it would always be less than 0 and cause an update of the nearest address, regardless of whether or not it is actually closer).