Refactored shutdown sequence to properly handle graceful shutdowns and allow blocking if required
Fixed shutdown sequence