Fix test failures and reported leaks