Refactor memory

Refactored memory.