fix logic errors in checking if expressions are valid