fix memleaks

*valgrind still reports errors, but does not show stack traces