Refactored code into class