Fixed it so that beginning and trailing NA rows are removed from the data frame before processing.