simplify error/close logic