Minor refactorings and a bug fix.