Overhaul the script. URL parsing; re-arrangement of commands, use Here Docs with echo statements, etc.