improved exit handling