working version of collisionavoiding version