Fix a number of collation issues, and do a fair bit of cleanup

The collation stuff is somewhat tentative as yet, since there's
as yet no easy way to test a lot of it.