Refactor permute