Some refactoring and design.