Some fixes and refactorings