Handle quitting and other stuff better