Refactor duplicated code