Refactor to improve performance