refactor termination event handling