Refactor, split in daemon and client mode for allow testing