Refactor to remove duplicate code