Some simplification and refactoring.