Some cleanup, and a fix to actually compute the primary sort field
correctly.