added if statements to make subsetting robust