permut code with checks