Moved _get_header_scopes call out of the for loop;
Some cleanup