Complete overhaul

Instead of doing incremental changes, I just went with a huge, one shot
overhaul from the production code that is in use at my place of
employment.