After considerable tinkering first draft of function best.R