Dramatic code cleanup and adoption of a more functional style.

This made the code look a lot nicer, but sadly some the functional
constructs make things slow.  Specifically using std::bind multiple
times and composing functions make things about 50% slower.

For now I'll leave the code in the cleaner slower form.