Center of gravity

I’ve searched for a long time for a center of gravity code, to see what I’ve been doing wrong. I can’t find one at all. And the one that I do have right now, it just makes it go continuessly somehow. (italcized because that’s one of the main problems)
There’s also another problem, it sometimes bounces back. Which the code isn’t telling it to at all. And it also sometimes goes to the top or left of the screen, something I don’t see why it happens.

grav is the movieclip it’s supposed to be attracted to.

Here’s the code so far, obviously it’s not done

	var xdist = Math.round(_root.grav._x - this._x); 
	var ydist = Math.round(_root.grav._y - this._y);
	var dfs = Math.round(Math.sqrt((xdist*xdist)+(ydist*ydist)));
	xspeed=(xspeed/1.1)+(gravity/dfs);
	yspeed=(yspeed/1.1)+(gravity/dfs);
	this._x+=xspeed/20
	this._y+=yspeed/20