Several improvements and refactorings