Rewrite new transpose code.

Rewrite the transpose function to more completely mimic ST3's behavior. Also
add more tests.