added shutdown hook
using Apache Daemon
remove code duplication