Refactored repeated code to make more usable