Added collision detection and more refactoring.