Refactor how we exit to workaround emscripten bug

See kripen/emscripten#2968