Fix ref/memory issues when loading packages

There are crashers due to use after free, in certain cases.