Fix checkValid() function