Build the URI string more intelligently