improve connectivity algorithm