simplified much of the code by repacking in memory