Fixed major memory leaks