remove duplicate code for permutedims