Fix package reloading

This fixes a number of bugs with package reloading.