First cut at 3D solution. Still buggy.