restructuring program to waste far less memory