Valgrinding the tests, fixing mem leaks + invalid access