Some fixes and refactorings.