added 2d changes to 3d code. not tested!