Refactoring the codes