Refactored precedence logic