Clean up properly for quit, EOF

Eg, stdout wasn't being properly restored for quit.