refactoried the code