Fix build with clang + various other fixes, based on reports by clang & valgrind. Also passing pointers instead of structs now in all cases.