Refactorings and fixes