reorganize code for rules