refactor for daemon running and shutdown