I would only do the queryURI work in the else block that uses the urlParts -- no point trying parse an URL that is not expected to be formatted in a way that it will be used in parts.