Overtook some fixes by johns.
Fixed free() of NULL pointers.