Added early termination conditions.