added daemonize support, some refactoring comments embbedded