Implemented a leaf counting function