Improvements and refactoring