Clean up source code

Ensure consistent spacing and formatting, isolate common functionality,
eliminate redundant code, etc . . .