better shutdown logic