Making a little more generic
Bringing in Carl's R-code.