Completely rewritten to properly parse all URLs