Refactoring.

simplified the code for the collision detection