Valgrind warnings

Fixes for "Conditional jump or move depends on uninitialised value(s)" and memleaks reported by valgrind.