Fixed bug for calculating neighborhood