 #define iSteps 16
 #define jSteps 8
 
-float rsi(vec3 r0, vec3 rd, float sr) {
-    // Simplified ray-sphere intersection that assumes
-    // the ray starts inside the sphere and that the
-    // sphere is centered at the origin. Always intersects.
+vec2 rsi(vec3 r0, vec3 rd, float sr) {
+    // ray-sphere intersection that assumes
+    // the sphere is centered at the origin.
+    // No intersection when result.x > result.y
     float a = dot(rd, rd);
     float b = 2.0 * dot(rd, r0);
     float c = dot(r0, r0) - (sr * sr);
-    return (-b + sqrt((b*b) - 4.0*a*c))/(2.0*a);
+    float d = (b*b) - 4.0*a*c;
+    if (d < 0.0) return vec2(1e5,-1e5);
+    return vec2(
+        (-b - sqrt(d))/(2.0*a),
+        (-b + sqrt(d))/(2.0*a)
+    );
 }
 
 vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {

    r = normalize(r);

    // Calculate the step size of the primary ray.
    float iStepSize = rsi(r0, r, rAtmos) / float(iSteps);

    // Initialize the primary ray time.
    float iTime = 0.0;

        iOdMie += odStepMie;

        // Calculate the step size of the secondary ray.
        float jStepSize = rsi(iPos, pSun, rAtmos).y / float(jSteps);

        // Initialize the secondary ray time.
        float jTime = 0.0;
