more validation and refactoring