Cleanup

The diff here isn't that helpful.  This is some basic cleanup and code de-duplication.