refactored validation logic