Some code clean-up and refactoring.