Refactor system shutdown