Add code for rule checking and delete duplicate code