Some improvements and refactoring