Refractored code and updated test class