optimize getNeighbor() function.