Refraction in Raytracing?

Warning: the following is a guess, not a certainty. I'd have to look at the code in more detail to be sure what's happening and why.

That said, it looks to me like your original code is basically simulating a concave lens instead of convex.

enter image description here

A convex lens is basically a magnifying lens, bringing light rays from a relatively small area into focus on a plane:

enter image description here

This also shows why the corrected code shows an upside-down image. The rays of light coming from the top on one side get projected to the bottom on the other (and vice versa).

Getting back to the concave lens though: a concave lens is a reducing lens that shows a wide angle of picture from in front of the lens:

enter image description here

If you look at the bottom right corner here, it shows what I suspect is the problem: especially with a high index of refraction, the rays of light trying to come into the lens intersect the edge of the lens itself. For all the angles wider than that, you're typically going to see a black ring, because the front edge of the lens is acting as a shade to prevent light from entering.

Increasing the index of refraction increases the width of that black ring, because the light is bent more, so a larger portion at the edges is intersecting the outer edge of the lens.

In case you care about how they avoid this with things like wide-angle camera lenses, the usual route is to use a meniscus lens, at least for the front element:

enter image description here

This isn't a panacea, but does at least prevent incoming light rays from intersecting the outer edge of the front lens element. Depending on exactly how wide an angle the lens needs to cover, it'll often be quite a bit less radical of a meniscus than this (and in some cases it'll be a plano-concave) but you get the general idea.

Final warning: of course, all of these are hand-drawn, and intended only to give general idea, not (for example) reflect the design of any particular lens, an element with any particular index of refraction, etc.