implemented all rules, with some duplication