First stab at term indexing.
Simplify term order and fix bug