restructuring for memory saving