Refactored the daemon to be less static and more testable.