Refactor code to avoid repetition