```\n            function getImageURIForHat(uint256 _hatId) public view returns (string memory) {\n                // check _hatId first to potentially avoid the `getHatLevel` call\n                      Hat memory hat = _hats[_hatId];\n                        string memory imageURI = hat.imageURI; // save 1 SLOAD\n                // if _hatId has an imageURI, we return it\n                            if (bytes(imageURI).length > 0) {\n                return imageURI;\n                }\n                // otherwise, we check its branch of admins\n                        uint256 level = getHatLevel(_hatId);\n                // but first we check if _hatId is a tophat, in which case we fall back to the global image uri\n                if (level == 0) return baseImageURI;\n                // otherwise, we check each of its admins for a valid imageURI\n                     uint256 id;\n                // already checked at `level` above, so we start the loop at `level - 1`\n                for (uint256 i = level - 1; i > 0;) {\n                     id = getAdminAtLevel(_hatId, uint8(i));\n                        hat = _hats[id];\n                            imageURI = hat.imageURI;\n                if (bytes(imageURI).length > 0) {\n                      return imageURI;\n                 }\n                // should not underflow given stopping condition is > 0\n                    unchecked {\n                         --i;\n                    }\n             }\n                // if none of _hatId's admins has an imageURI of its own, we \n            again fall back to the global image uri\n                return baseImageURI;\n            }\n```\n