added validation and refactor