Added collision detection and gradient descent