Add support for graceful shutdown (see #1)