refactor for memory