Memory code refactor