Improved validation logic