Refactored a lot of code.

Mostly to move toward a different way of searching (defining
equivalence classes of train tracks, finding maps between these, and
finding closed loops).