paint the weights of each bone... what its doing wrong is calculating the weights according to distance from the joing , so you can easily get verts attracted to compleatly the wrong joint.