Separated some duplicated code.