Better auto shutdown logic.

Fixes #1054.
Style: removed some superfluous whitespace.
Merge pull request #1064 from hjoliver/1054.auto-shutdown

1054. better auto shutdown logic