fixed critical memory bugs