Work on bad accesses and then memory leaks.

This allows us to fix an invalid function pointer cast.