Refactored branch diagram code