rewrite longterm memory logic