moved memory handling to mem.c, mem.h