I wonder whether some of this URL operations might be easier with `urlparse` http://docs.python.org/2/library/urlparse.html