Updated to handle any number of neighbors.