better handling of shutdown