Refectoring to remove duplicated code