Support reference uri & fix gobs of C code

Already talked about the gotos earlier, but FTR, it's actually factored out code. :)

If I were starting this from scratch, it'd look quite a bit different... (step 1...C++ so I have auto_ptr/unique_ptr and can thus remove this cleanup mess.)