Refactoring neighbour alorithm