Rework rule optimization and expand tests.