Refactor for better memory useage