Refactor shutdown handler to catch exit/die and send apropriate headers