Refactored the signal handling so that we shut down in an orderly