Refactor memory.c