Finished algorithm with neighbours