Exit shell when window closes.
Fix memory leak/crashes with read loop.