This is the actual fix.  The rest should just be whitespace changes and code cleanup.