Fix all major memory leaks

Fixed major memory leak with rawMaterials not releasing, and some other memory bugs.