Fix some bugs on quit