Fix memory bugs introduced in dffa438e99