refactor heading code to only use two functions, no repeated code