Better bounding box algorithm