Refactor branching logic