Fixed double free issue in pua module

Also fix a couple of related memory leaks