Simplified code using frequencies function and made two defs private