Code consolidation and formatting cleanup