Fix some more memleaks and add memleak testsuite