Handles missing values correctly now (which kind of is the point of this function....)