Improved quadtree for collision detection, and various optimizations