Fix memory error with nested each