Refactor tomato memory