refactorization and improvement of collision detection