Rewriting - not tested
Complete rewrite

Changed the way how endpoints and connections are
managed.