Refactor for better mem usage