Fix rules main bug and clean up some code