refine mem reading codes
refactor -- not tested yet