get-neighbours function modified for general case