Clean up and fix for OS X's behavior of restoring windows on crash.

- Added comments and cleaned up the code to conform to style guide.
- Refactored pieces of session wrapper.
- Bugfix for handling two errors in a row - we now flush stderr buffer
  more often.

BUG=
R=scudette@gmail.com

Review URL: https://codereview.appspot.com/106850044