slight rework to memory counting