refactored and added validation