Minor refactorings and fixes