Fixing errors in validation function