shutdown timeout problems and races, fixes #406
Fix shutdown problems, Closes #406