Memory allocation fixes