fix termination logic