Work on bad accesses and then memory leaks.

Just a drive-by comment (I've done similar things in my PRs) but I've seen that freeing NULL should be fine: 
http://stackoverflow.com/questions/1938735/does-freeptr-where-ptr-is-null-corrupt-memory