Refactor the kernel shutdown procedures