Consolidate HTML building logic