added collision detection code