Final refactors and touchups (some variable renaming as well).