daemon: refactor and cleanup