Refactorized the code