Handle system exit

Inspired by #228. Added rspec tests.

Also include a missing commit for 0.9.2 - 3286581cb9754fcaf80e78a8f80cbd5da436067a