Various fixes and refactoring.