How to limit this object rotation script to objects being mouseover-ed?

I have this code that rotates a cube with the mouse on rollover and then slows to a stop. I have the script applied to several cubes in the scene. What happens is, when I mouseover the first cube it works perfectly. However, when I move to a second cube, the second cube rotates, but the first cube rotates as well.

The end result being that every cube I mouseover becomes locked in the rotation state, instead of being ignored once the mouse leaves it. How can I have so that only the cube the mouse is over is affected? It works perfectly when only applied to a single cube. Here is the code I am using and dropping onto each cube (I did not write this code, I just tweaked it. I am a beginner) :

var rotationSpeed = 10.0;
var lerpSpeed = 1.0;

private var speed = new Vector3();
private var avgSpeed = new Vector3();
private var dragging = false;
private var moveout = false;
private var targetSpeedX = new Vector3();

function OnMouseOver()
{
dragging = true;
}

function OnMouseOut()
{
moveout = true;
}

function Update () {

if (dragging)
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//var layerMask = 1 << 8;
if (Physics.Raycast (ray, 1000))
{
speed = new Vector3(-Input.GetAxis (“Mouse X”), Input.GetAxis(“Mouse Y”), 0);
avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
print (“The ray hit the player”);

}

else {
if (moveout) {
speed = avgSpeed;
dragging = false;
}
var i = Time.deltaTime * lerpSpeed;
speed = Vector3.Lerp( speed, Vector3.zero, i);
}

}
transform.Rotate(Vector3.up, speed.x * rotationSpeed, Space.World);
transform.Rotate(Vector3.right, -speed.y * rotationSpeed, Space.World);

}

Is it possible to do something with a “layerMask”? I was messing with putting all the cubes into a layer, then putting the script on an object outside the layer, but I don’t know enough code to make that work. I’m not sure that would fix the above issue, it might just make it so I don’t have to apply it to every cube individually.