Minor refactoring and fixes.