Refactor helper function