Added more general function for permutations