Refactoring and more improvements.