Improvements to rebuildUrl method