Imporved connectivity computation function.

Now there are less code duplication and the function is more readable.