factorize function connect
Reorganize code