Some fixes and minor refactorings