added improved collision detection