Added support for max distance.

So: This is how I understand max distance to work. It needs to be
pretty heavily tweaked probably.  Also: this still hasn't been
tested live, which is unfortunate.