enhance the logic for task termination