Rework shutdown sequence to close #391