fixed function countLeaves