Refactor code to be less repetitive