Mostly rewritten the daemonize function following Stevens' example