Fix algorithm for neighbor count