New optimized version with validation