add function to finish daemon in an correct way