moved RPY computation to a separate function.