Fixing major memory leaks