improve exit handling