Refactor and fix get_sorts_with_header