Minor fixes and refactoring.