created 'connectNodes' function