move daemon functions to separate file and add tests