Clean up daemon-driver code

The plan is to replace the current "main" code with some variation
of the daemon-driver code in the near future. Also, made it so that
stdout and stderr are flushed before the daemon is deleted.