implemented graceful shutdown