Merge "Clean code a bit and fix a couple of small bugs in ext-refs" into nextgenv2