tried to improve the exit handling