shutdown hook to call flush