Stop and close devices before exiting