Refactored some code to remove duplication
Refactored some code to remove duplication