refactor freq; fix permute