Fixes memory leaks