[Issue #7] Add support for graceful shutdowns. Incorporated review feedback.