complete rewrite of the rules engine