refactored headings loop