Refactory gateway to remove duplicated code