Clang warning fixes

Some straightforward fixes to silence the warnings from -wall with clang. No real intelligence applied; I think the use of class/struct could be normalised much more. This is as a precursor to tracking down a crash on certain inputs when compiled with clang.

Bonus: A simple use after free bug corrected.