Some minor fixes and refactoring.