added middleware cleanup on shutdown