improved mem management in grpany code