Fixed function for finding total number of branches