Improved validation and refactored