add some code for graceful quit