refactored some of the repeated code