tweaked nested for loops of transpose