general cleanup/refactoring

Rename variables, rewrite loops, change expressions/spacing for
readability, and reuse code where possible (`bigcmp). Most
importantly, put the logic for context-labeling into its own function.
This is a big improvement because previously that portion of code was
a complete mystery.

Document mystery variables in AM.xs where possible, contributing to

Update TODO.txt when ideas come.