Refactored and fixed validation