fixed logic issues in scan_neighbors method