Refactor check neighbor for loop