URL parsing and formatting (not tested)
some corrections and changes