Fixed a few exit crashes, added unit tests to cover them
Merge branch 'crash-fixes' into develop