Overhaul, split into 2d and 3d versions.