Refactor rule building mechanism.