Refactored code to remove redundancies