Improved check for valid key