better shutdown; minor refactoring