Index: lab5/graphics.c
===================================================================
--- lab5/graphics.c	(revision 98)
+++ lab5/graphics.c	(revision 99)
@@ -7,46 +7,71 @@
 #include <stdio.h>
 
 static void *video_mem; /** @brief Frame-buffer VM address */
-static vbe_mode_info_t vbe_mem_info;
-//static mmap_t mem_map;
+//static vbe_mode_info_t vbe_mem_info;
+static mmap_t mem_map;
 
-int (set_graphics_mode)(uint16_t mode) {
-    int r;
+int (get_permission)(unsigned int base_addr, unsigned int size) {
     struct minix_mem_range mmr;
-    mmr.mr_base =  0x0;
-    mmr.mr_limit = 0xFFFFF;
+    mmr.mr_base = base_addr;
+    mmr.mr_limit = base_addr + size;
+    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
+}
 
-    struct reg86 reg_86;
+int (get_permissions_first_mbyte)(void) {
+    return get_permission(MBYTE_BASE, MBYTE_SIZE);
+}
 
-    if ((r = sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr)))
-        panic("sys_privctl (ADD MEM) failed: %d\n", r);
+int (vbe_get_mode_information)(uint16_t mode, vbe_mode_info_t *vmi_p) {
+    memset(vmi_p, 0, sizeof(vbe_mode_info_t)); // reset values
 
+    struct reg86 reg_86;
+    memset(&reg_86, 0, sizeof(reg_86)); // reset struct
 
-    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // clear mem_info to initialize it
+    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
 
-    if (vbe_get_mode_info(mode, &vbe_mem_info)) {
-        printf("vbe_get_mode_info: Failed to get VBE Mode Info for mode %x\n", mode);
-        return LCF_ERROR;
+    reg_86.intno = VC_BIOS_SERV;
+    reg_86.ah = VBE_CALL;
+    reg_86.al = VBE_MD_INFO;
+    reg_86.cx = mode;
+    reg_86.es = PB2BASE(mem_map.phys);
+    reg_86.di = PB2OFF(mem_map.phys);
+    // BIOS CALL
+    if (sys_int86(&reg_86)) {
+        printf("%s: sys_int86 failed\n", __func__);
+        if (free_memory()) {
+            printf("%s: lm_free failed\n", __func__);
+            return LCF_ERROR;
+        }
+        return BIOS_CALL_ERROR;
     }
-    unsigned int vram_base = vbe_mem_info.PhysBasePtr;
-    unsigned int vram_size = vbe_mem_info.XResolution * vbe_mem_info.YResolution * (vbe_mem_info.BitsPerPixel >> 3);
 
-    // Allow memory mapping
-    mmr.mr_base =  vram_base;
-    mmr.mr_limit = vram_base + vram_size;
+    vmi_p = memcpy((void*)vmi_p, (void*)virtual_addr, mem_map.size);
+    return SUCCESS;
+}
 
-    if ((r = sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr)))
-        panic("sys_privctl (ADD MEM) failed: %d\n", r);
+int (map_vram)(unsigned int vram_base, unsigned int vram_size) {
+    int r;
+    if ((r = get_permission(vram_base, vram_size)))
+        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
 
-    // Map memory
+    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
 
-    video_mem = vm_map_phys(SELF, (void *)mmr.mr_base, vram_size);
-
     if (video_mem == MAP_FAILED)
-        panic("Error: couldn't map video memory.");
+        panic("%s: couldn't map video memory.", __func__);
 
+    return SUCCESS;
+}
+
+int (free_memory)(void) {
+    return lm_free(&mem_map);
+}
+
+int (set_graphics_mode)(uint16_t mode) {
+    struct reg86 reg_86;
+
     memset(&reg_86, 0, sizeof(reg_86)); // reset struct
 
+    // Set Reg86
     reg_86.intno = VC_BIOS_SERV;
     reg_86.ah = VBE_CALL;
     reg_86.al = SET_VBE_MD;
@@ -53,10 +78,10 @@
     reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
 
     // BIOS CALL
-
     if (sys_int86(&reg_86)) {
         printf("%s: sys_int86 failed\n", __func__);
         return BIOS_CALL_ERROR;
     }
+
     return SUCCESS;
 }
Index: lab5/graphics.h
===================================================================
--- lab5/graphics.h	(revision 98)
+++ lab5/graphics.h	(revision 99)
@@ -1,8 +1,19 @@
 #ifndef GRAPHICS_H_INCLUDED
 #define GRAPHICS_H_INCLUDED
 
+#include <lcom/lcf.h>
 #include <stdint.h>
 
+int (get_permission)(unsigned int base_addr, unsigned int size);
+
+int (get_permissions_first_mbyte)(void);
+
+int (vbe_get_mode_information)(uint16_t mode, vbe_mode_info_t *vmi_p);
+
+int (map_vram)(unsigned int vram_base, unsigned int vram_size);
+
+int (free_memory)(void);
+
 /**
  * @brief
  * @param mode
Index: lab5/graphics_macros.h
===================================================================
--- lab5/graphics_macros.h	(revision 98)
+++ lab5/graphics_macros.h	(revision 99)
@@ -4,6 +4,9 @@
 #define VC_BIOS_SERV  0x10 /** @brief TODO */
 #define VBE_CALL      0x4F /** @brief TODO */
 
+#define MBYTE_BASE  0x0
+#define MBYTE_SIZE  0xFFFFF
+
 // Graphics Functions
 #define VBE_CTRL_INFO       0x00 /** @brief Get VBE Controller Information */
 #define VBE_MD_INFO         0x01 /** @brief Get VBE Mode Information */
Index: lab5/lab5.c
===================================================================
--- lab5/lab5.c	(revision 98)
+++ lab5/lab5.c	(revision 99)
@@ -35,12 +35,45 @@
 }
 
 int(video_test_init)(uint16_t mode, uint8_t delay) {
+    int r;
+    if ((r = get_permissions_first_mbyte()))
+        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
 
-    set_graphics_mode(mode);
+    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__);
+        return 1;
+    };
 
+    vbe_mode_info_t vbe_mem_info;
+
+    if (vbe_get_mode_information(mode, &vbe_mem_info)) {
+        printf("%s: failed to get information for mode %x.\n", __func__, mode);
+        if (vg_exit())
+            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        return 1;
+    }
+
+    unsigned int vram_base = vbe_mem_info.PhysBasePtr;
+    unsigned int vram_size = vbe_mem_info.XResolution * vbe_mem_info.YResolution * ((vbe_mem_info.BitsPerPixel + 7) >> 3);
+
+    map_vram(vram_base, vram_size); // if function fails it aborts program
+
     tickdelay(micros_to_ticks(delay*1e6));
 
-    vg_exit();
+    if (vg_exit()) {
+        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
+        if (free_memory())
+            printf("%s: lm_free failed\n", __func__);
+        return 1;
+    }
+
+    if (free_memory()) {
+        printf("%s: lm_free failed\n", __func__);
+        return 1;
+    }
+
     return 0;
 }
 
