Misc refactoring and correcting.