Fixed collision detection and resolution to work correctly.