Various fixes, refactorings, improvements.