Fixes and refactorings