Updated to include possibility that a factor can have NA as a level