Refactored code slightly