rewrite transform: 10x faster, fixed bugs