Refactor for optimization