merge daemon and lib

Merge daemon and lib, pluss other changes.