Refactoring dom code