Massive code cleanup and formatting