Finished ugly implementation of whole thing.

Going to work on refining it, because it is really really ugly. Though
there is a certain elegance underneath the surface.