Refactor repeated code