add better exit handler to avoid such problems in the future