Better shutdown sequence. Fixes #9