More warning and memleak fixes

Just as the title suggests.
Cuts down scanbuild warnings.