Added graceful shutdown handling.