Refactor the code with object oriented method