Getting Proper Occlusions

I have rendered a matrix of cubes within a wireframe bounding cube but am having 2 main issues:

  1. the wireframe cube should not be occluded when in the foreground, but it is being. ( I got it’s background edges to be occluded by rendering it before the internal cubes![Cube1|556x500]

  2. when the whole scene is rotated so that what was previously in the background comes to the foreground, the internal cubes that are now in the foreground are NOT occluding the cubes that logically are behind them.

can someone please suggest how i can resolve this, to make it appear that the cubes within the wireframe cube always appear as if they are totally inside the wireframe.

thankyou, please see attached image for clarity.