Index: lab5/graphics.c
===================================================================
--- lab5/graphics.c	(revision 137)
+++ lab5/graphics.c	(revision 138)
@@ -36,11 +36,10 @@
     reg_86.es = PB2BASE(mem_map.phys);
     reg_86.di = PB2OFF(mem_map.phys);
     // BIOS CALL
-    if (sys_int86(&reg_86)) {
+    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
         printf("%s: sys_int86 failed\n", __func__);
         if (free_memory_map()) {
             printf("%s: lm_free failed\n", __func__);
-            return LCF_ERROR;
         }
         return BIOS_CALL_ERROR;
     }
@@ -73,11 +72,10 @@
     reg_86.es = PB2BASE(controller_map.phys);
     reg_86.di = PB2OFF(controller_map.phys);
     // BIOS CALL
-    if (sys_int86(&reg_86)) {
+    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
         printf("%s: sys_int86 failed\n", __func__);
-        if (free_memory_map()) {
+        if (!lm_free(&controller_map)) {
             printf("%s: lm_free failed\n", __func__);
-            return LCF_ERROR;
         }
         return BIOS_CALL_ERROR;
     }
@@ -117,11 +115,11 @@
     virtual_ptr = phys_ptr + virtual_base;
     info_p->OEMProductRevPtr = (char*)(virtual_ptr);
 
-    lm_free(&controller_map);
+    if (!lm_free(&controller_map)) {
+        printf("%s: lm_free failed\n", __func__);
+        return LCF_ERROR;
+    }
 
-
-
-
     return SUCCESS;
 }
 
@@ -157,13 +155,21 @@
     int r;
     unsigned int vram_base = get_phys_addr();
     unsigned int vram_size = get_vram_size();
-    if ((r = get_permission(vram_base, vram_size)))
+    if ((r = get_permission(vram_base, vram_size))) {
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
+    }
 
     video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
 
-    if (video_mem == MAP_FAILED)
+    if (video_mem == MAP_FAILED) {
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         panic("%s: couldn't map video memory.", __func__);
+    }
 
     return SUCCESS;
 }
@@ -184,7 +190,7 @@
     reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
 
     // BIOS CALL
-    if (sys_int86(&reg_86)) {
+    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
         printf("%s: sys_int86 failed\n", __func__);
         return BIOS_CALL_ERROR;
     }
Index: lab5/lab5.c
===================================================================
--- lab5/lab5.c	(revision 137)
+++ lab5/lab5.c	(revision 138)
@@ -53,8 +53,12 @@
 
     if (set_graphics_mode(mode)) {
         printf("%s: failed to set graphic mode %x.\n", __func__, mode);
-        if (vg_exit())
+        if (vg_exit()) {
             printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     };
 
@@ -90,16 +94,28 @@
 
     if (set_graphics_mode(mode)) {
         printf("%s: failed to set graphic mode %x.\n", __func__, mode);
-        if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        if (vg_exit()) {
+            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     };
 
-    if (vg_draw_rectangle(x, y, width, height, color)) {
-        if (vg_exit()) {
-            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-            if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
+    uint16_t w = (x + width > get_XRes()) ? (get_XRes() - x) : (width);
+    uint16_t h = (y + height > get_YRes()) ? (get_YRes() - y) : (height);
+
+    if (x < get_XRes() && y < get_YRes()) {
+        if (vg_draw_rectangle(x, y, w, h, color)) {
+            if (vg_exit()) {
+                printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+            }
+            if (free_memory_map()) {
+                printf("%s: lm_free failed\n", __func__);
+            }
+            return 1;
         }
-        return 1;
     }
 
     /// loop stuff
@@ -112,8 +128,10 @@
     if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
         if (vg_exit()) {
             printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-            if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
         }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     }
     /// cycle
@@ -143,8 +161,10 @@
     if (unsubscribe_interrupt(&kbc_id)) {
         if (vg_exit()) {
             printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-            if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
         }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     };
 
@@ -177,6 +197,9 @@
     if (set_graphics_mode(mode)) {
         printf("%s: failed to set graphic mode %x.\n", __func__, mode);
         if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     };
 
@@ -194,12 +217,21 @@
                 B = (GET_BLU(first) + (col+row)*step) % (1 << get_BlueMaskSize());
                 color = SET_COLOR(R,G,B);
             }
-            if (vg_draw_rectangle(col*W,row*H,W,H,color)) {
-                if (vg_exit()) {
-                    printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-                    if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
+            uint16_t x = col * W;
+            uint16_t y = row * H;
+            uint16_t w = (x + W > get_XRes()) ? (get_XRes() - x) : (W);
+            uint16_t h = (y + H > get_YRes()) ? (get_YRes() - y) : (H);
+
+            if (x < get_XRes() && y < get_YRes()) {
+                if (vg_draw_rectangle(x,y,w,h,color)) {
+                    if (vg_exit()) {
+                        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+                    }
+                    if (free_memory_map()) {
+                        printf("%s: lm_free failed\n", __func__);
+                    }
+                    return 1;
                 }
-                return 1;
             }
         }
     }
@@ -213,8 +245,10 @@
     if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
         if (vg_exit()) {
             printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-            if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
         }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     }
     /// cycle
@@ -244,8 +278,10 @@
     if (unsubscribe_interrupt(&kbc_id)) {
         if (vg_exit()) {
             printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-            if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
         }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     };
 
@@ -278,6 +314,9 @@
     if (set_graphics_mode(INDEXED_1024_768)) {
         printf("%s: failed to set graphic mode %x.\n", __func__, INDEXED_1024_768);
         if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     };
 
@@ -295,8 +334,10 @@
     if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
         if (vg_exit()) {
             printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-            if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
         }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     }
     /// cycle
@@ -326,8 +367,10 @@
     if (unsubscribe_interrupt(&kbc_id)) {
         if (vg_exit()) {
             printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-            if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
         }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     };
 
@@ -360,6 +403,9 @@
     if (set_graphics_mode(INDEXED_1024_768)) {
         printf("%s: failed to set graphic mode %x.\n", __func__, INDEXED_1024_768);
         if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     };
 
@@ -370,7 +416,13 @@
     uint8_t timer_irq_bit = 0;
     int timer_id = 0;
     int timer_irq = BIT(timer_irq_bit);
-    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) return 1;
+    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) {
+        if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
+        return 1;
+    }
 
     no_interrupts = 0;
     /// Keyboard interrupt handling
@@ -380,8 +432,10 @@
     if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
         if (vg_exit()) {
             printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-            if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
         }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     }
     ///
@@ -442,12 +496,22 @@
     if (unsubscribe_interrupt(&kbc_id)) {
         if (vg_exit()) {
             printf("%s: vg_exit failed to exit to text mode.\n", __func__);
-            if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
         }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
         return 1;
     };
 
-    if (unsubscribe_interrupt(&timer_id)) return 1;
+    if (unsubscribe_interrupt(&timer_id)) {
+        if (vg_exit()) {
+            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        }
+        if (free_memory_map()) {
+            printf("%s: lm_free failed\n", __func__);
+        }
+        return 1;
+    }
 
     if (vg_exit()) {
         printf("%s: vg_exit failed to exit to text mode.\n", __func__);
@@ -467,9 +531,15 @@
 
     vg_vbe_contr_info_t cntrl_info;
 
-    vbe_get_controller_information(&cntrl_info);
+    if (vbe_get_controller_information(&cntrl_info)) {
+        printf("%s: vbe_get_controller_information failed to get controller info.\n", __func__);
+        return 1;
+    }
 
-    vg_display_vbe_contr_info(&cntrl_info);
+    if (vg_display_vbe_contr_info(&cntrl_info)) {
+        printf("%s: vg_display_vbe_contr_info failed to display controller info.\n", __func__);
+        return 1;
+    }
 
     return 0;
 }
Index: lab5/sprite.c
===================================================================
--- lab5/sprite.c	(revision 137)
+++ lab5/sprite.c	(revision 138)
@@ -44,7 +44,9 @@
 void sprite_draw(const sprite_t *p){
     for (int i = 0; i < p->w; i++) {
         for (int j = 0; j < p->h; j++) {
-            set_pixel(p->x + i, p->y + j, p->map[i + j * p->w]);
+            if (p->x + i < get_XRes() && p->y + j < get_YRes()) {
+                set_pixel(p->x + i, p->y + j, p->map[i + j * p->w]);
+            }
         }
     }
 }
