Robert's BPM function, refractored