Updated code for SwapMemory