move leaf count extraction into own template