Work on bad accesses and then memory leaks.

The cast eliminates a warning and doesn't cause any problems as used.