Adding detectNeighbors Function