improved simplify algorithm