Fixed some major memory bugs causing runtime errors