Improvements and refactorings to the various pieces.