1054. better auto shutdown logic

This closes #1054.  It also tidies the auto shutdown logic, and (in a separate commit) does some whitespace style rectification.