refactor daemon logic