Some refactoring and clean up.