Complete rewrite

Changed the way how endpoints and connections are
managed.