Moved tab count logic to the separate function