Bugfixes and refactoring dom code