Reduction of code repetition and a few other organizational tweaks.