Index: kernel/genarch/src/drivers/ega/ega.c
===================================================================
--- kernel/genarch/src/drivers/ega/ega.c	(revision ba7371f9d732a62dea4383ffb0e509bccd9f4e4a)
+++ kernel/genarch/src/drivers/ega/ega.c	(revision da1bafb8cf9a3b3be8ef21bc114daaa476a85190)
@@ -63,5 +63,5 @@
 
 typedef struct {
-	SPINLOCK_DECLARE(lock);
+	IRQ_SPINLOCK_DECLARE(lock);
 	
 	uint32_t cursor;
@@ -71,6 +71,6 @@
 } ega_instance_t;
 
-static void ega_putchar(outdev_t *dev, wchar_t ch, bool silent);
-static void ega_redraw(outdev_t *dev);
+static void ega_putchar(outdev_t *, wchar_t, bool);
+static void ega_redraw(outdev_t *);
 
 static outdev_operations_t egadev_ops = {
@@ -540,6 +540,5 @@
 	ega_instance_t *instance = (ega_instance_t *) dev->data;
 	
-	ipl_t ipl = interrupts_disable();
-	spinlock_lock(&instance->lock);
+	irq_spinlock_lock(&instance->lock, true);
 	
 	switch (ch) {
@@ -564,6 +563,5 @@
 	ega_move_cursor(instance, silent);
 	
-	spinlock_unlock(&instance->lock);
-	interrupts_restore(ipl);
+	irq_spinlock_unlock(&instance->lock, true);
 }
 
@@ -572,6 +570,5 @@
 	ega_instance_t *instance = (ega_instance_t *) dev->data;
 	
-	ipl_t ipl = interrupts_disable();
-	spinlock_lock(&instance->lock);
+	irq_spinlock_lock(&instance->lock, true);
 	
 	memcpy(instance->addr, instance->backbuf, EGA_VRAM_SIZE);
@@ -579,6 +576,5 @@
 	ega_show_cursor(instance, silent);
 	
-	spinlock_unlock(&instance->lock);
-	interrupts_restore(ipl);
+	irq_spinlock_unlock(&instance->lock, true);
 }
 
@@ -598,5 +594,5 @@
 	egadev->data = instance;
 	
-	spinlock_initialize(&instance->lock, "*ega_lock");
+	irq_spinlock_initialize(&instance->lock, "*ega.instance.lock");
 	
 	instance->base = base;
Index: kernel/genarch/src/mm/as_ht.c
===================================================================
--- kernel/genarch/src/mm/as_ht.c	(revision ba7371f9d732a62dea4383ffb0e509bccd9f4e4a)
+++ kernel/genarch/src/mm/as_ht.c	(revision da1bafb8cf9a3b3be8ef21bc114daaa476a85190)
@@ -30,8 +30,8 @@
  * @{
  */
- 
+
 /**
  * @file
- * @brief	Address space functions for global page hash table.
+ * @brief Address space functions for global page hash table.
  */
 
@@ -46,9 +46,9 @@
 #include <synch/mutex.h>
 
-static pte_t *ht_create(int flags);
-static void ht_destroy(pte_t *page_table);
+static pte_t *ht_create(unsigned int);
+static void ht_destroy(pte_t *);
 
-static void ht_lock(as_t *as, bool lock);
-static void ht_unlock(as_t *as, bool unlock);
+static void ht_lock(as_t *, bool);
+static void ht_unlock(as_t *, bool);
 
 as_operations_t as_ht_operations = {
@@ -68,6 +68,7 @@
  *
  * @return Returns NULL.
+ *
  */
-pte_t *ht_create(int flags)
+pte_t *ht_create(unsigned int flags)
 {
 	if (flags & FLAG_AS_KERNEL) {
@@ -75,4 +76,5 @@
 		mutex_initialize(&page_ht_lock, MUTEX_PASSIVE);
 	}
+	
 	return NULL;
 }
@@ -83,4 +85,5 @@
  *
  * @param page_table This parameter is ignored.
+ *
  */
 void ht_destroy(pte_t *page_table)
@@ -94,6 +97,7 @@
  * Interrupts must be disabled.
  *
- * @param as Address space.
+ * @param as   Address space.
  * @param lock If false, do not attempt to lock the address space.
+ *
  */
 void ht_lock(as_t *as, bool lock)
@@ -101,4 +105,5 @@
 	if (lock)
 		mutex_lock(&as->lock);
+	
 	mutex_lock(&page_ht_lock);
 }
@@ -109,10 +114,12 @@
  * Interrupts must be disabled.
  *
- * @param as Address space.
+ * @param as     Address space.
  * @param unlock If false, do not attempt to lock the address space.
+ *
  */
 void ht_unlock(as_t *as, bool unlock)
 {
 	mutex_unlock(&page_ht_lock);
+	
 	if (unlock)
 		mutex_unlock(&as->lock);
Index: kernel/genarch/src/mm/as_pt.c
===================================================================
--- kernel/genarch/src/mm/as_pt.c	(revision ba7371f9d732a62dea4383ffb0e509bccd9f4e4a)
+++ kernel/genarch/src/mm/as_pt.c	(revision da1bafb8cf9a3b3be8ef21bc114daaa476a85190)
@@ -33,5 +33,5 @@
 /**
  * @file
- * @brief	Address space functions for 4-level hierarchical pagetables.
+ * @brief Address space functions for 4-level hierarchical pagetables.
  */
 
@@ -47,9 +47,9 @@
 #include <arch.h>
 
-static pte_t *ptl0_create(int flags);
-static void ptl0_destroy(pte_t *page_table);
+static pte_t *ptl0_create(unsigned int);
+static void ptl0_destroy(pte_t *);
 
-static void pt_lock(as_t *as, bool lock);
-static void pt_unlock(as_t *as, bool unlock);
+static void pt_lock(as_t *, bool);
+static void pt_unlock(as_t *, bool);
 
 as_operations_t as_pt_operations = {
@@ -67,36 +67,38 @@
  *
  * @return New PTL0.
+ *
  */
-pte_t *ptl0_create(int flags)
+pte_t *ptl0_create(unsigned int flags)
 {
-	pte_t *src_ptl0, *dst_ptl0;
-	ipl_t ipl;
-	int table_size;
-
-	dst_ptl0 = (pte_t *) frame_alloc(PTL0_SIZE, FRAME_KA);
-	table_size = FRAME_SIZE << PTL0_SIZE;
-
-	if (flags & FLAG_AS_KERNEL) {
+	pte_t *dst_ptl0 = (pte_t *) frame_alloc(PTL0_SIZE, FRAME_KA);
+	size_t table_size = FRAME_SIZE << PTL0_SIZE;
+	
+	if (flags & FLAG_AS_KERNEL)
 		memsetb(dst_ptl0, table_size, 0);
-	} else {
-		uintptr_t src, dst;
-	
+	else {
 		/*
 		 * Copy the kernel address space portion to new PTL0.
+		 *
 		 */
-		 
-		ipl = interrupts_disable();
-		mutex_lock(&AS_KERNEL->lock);		
-		src_ptl0 = (pte_t *) PA2KA((uintptr_t) AS_KERNEL->genarch.page_table);
-
-		src = (uintptr_t) &src_ptl0[PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)];
-		dst = (uintptr_t) &dst_ptl0[PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)];
-
+		
+		ipl_t ipl = interrupts_disable();
+		mutex_lock(&AS_KERNEL->lock);
+		
+		pte_t *src_ptl0 =
+		    (pte_t *) PA2KA((uintptr_t) AS_KERNEL->genarch.page_table);
+		
+		uintptr_t src =
+		    (uintptr_t) &src_ptl0[PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)];
+		uintptr_t dst =
+		    (uintptr_t) &dst_ptl0[PTL0_INDEX(KERNEL_ADDRESS_SPACE_START)];
+		
 		memsetb(dst_ptl0, table_size, 0);
-		memcpy((void *) dst, (void *) src, table_size - (src - (uintptr_t) src_ptl0));
+		memcpy((void *) dst, (void *) src,
+		    table_size - (src - (uintptr_t) src_ptl0));
+		
 		mutex_unlock(&AS_KERNEL->lock);
 		interrupts_restore(ipl);
 	}
-
+	
 	return (pte_t *) KA2PA((uintptr_t) dst_ptl0);
 }
@@ -107,8 +109,9 @@
  *
  * @param page_table Physical address of PTL0.
+ *
  */
 void ptl0_destroy(pte_t *page_table)
 {
-	frame_free((uintptr_t)page_table);
+	frame_free((uintptr_t) page_table);
 }
 
@@ -118,6 +121,7 @@
  * Interrupts must be disabled.
  *
- * @param as Address space.
+ * @param as   Address space.
  * @param lock If false, do not attempt to lock the address space.
+ *
  */
 void pt_lock(as_t *as, bool lock)
@@ -132,6 +136,7 @@
  * Interrupts must be disabled.
  *
- * @param as Address space.
+ * @param as     Address space.
  * @param unlock If false, do not attempt to unlock the address space.
+ *
  */
 void pt_unlock(as_t *as, bool unlock)
Index: kernel/genarch/src/mm/page_ht.c
===================================================================
--- kernel/genarch/src/mm/page_ht.c	(revision ba7371f9d732a62dea4383ffb0e509bccd9f4e4a)
+++ kernel/genarch/src/mm/page_ht.c	(revision da1bafb8cf9a3b3be8ef21bc114daaa476a85190)
@@ -33,5 +33,5 @@
 /**
  * @file
- * @brief	Virtual Address Translation (VAT) for global page hash table.
+ * @brief Virtual Address Translation (VAT) for global page hash table.
  */
 
@@ -52,12 +52,11 @@
 #include <align.h>
 
-static size_t hash(unative_t key[]);
-static bool compare(unative_t key[], size_t keys, link_t *item);
-static void remove_callback(link_t *item);
-
-static void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
-    int flags);
-static void ht_mapping_remove(as_t *as, uintptr_t page);
-static pte_t *ht_mapping_find(as_t *as, uintptr_t page);
+static size_t hash(unative_t[]);
+static bool compare(unative_t[], size_t, link_t *);
+static void remove_callback(link_t *);
+
+static void ht_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
+static void ht_mapping_remove(as_t *, uintptr_t);
+static pte_t *ht_mapping_find(as_t *, uintptr_t);
 
 /**
@@ -65,10 +64,12 @@
  * after address space lock and after any address space area
  * locks.
+ *
  */
 mutex_t page_ht_lock;
 
-/**
- * Page hash table.
+/** Page hash table.
+ *
  * The page hash table may be accessed only when page_ht_lock is held.
+ *
  */
 hash_table_t page_ht;
@@ -93,4 +94,5 @@
  *
  * @return Index into page hash table.
+ *
  */
 size_t hash(unative_t key[])
@@ -98,5 +100,4 @@
 	as_t *as = (as_t *) key[KEY_AS];
 	uintptr_t page = (uintptr_t) key[KEY_PAGE];
-	size_t index;
 	
 	/*
@@ -104,6 +105,7 @@
 	 * of occurring. Least significant bits of VPN compose the
 	 * hash index.
-	 */
-	index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES - 1));
+	 *
+	 */
+	size_t index = ((page >> PAGE_WIDTH) & (PAGE_HT_ENTRIES - 1));
 	
 	/*
@@ -111,4 +113,5 @@
 	 * similar addresses. Least significant bits compose the
 	 * hash index.
+	 *
 	 */
 	index |= ((unative_t) as) & (PAGE_HT_ENTRIES - 1);
@@ -119,28 +122,28 @@
 /** Compare page hash table item with page and/or address space.
  *
- * @param key Array of one or two keys (i.e. page and/or address space).
+ * @param key  Array of one or two keys (i.e. page and/or address space).
  * @param keys Number of keys passed.
  * @param item Item to compare the keys with.
  *
  * @return true on match, false otherwise.
+ *
  */
 bool compare(unative_t key[], size_t keys, link_t *item)
 {
-	pte_t *t;
-
 	ASSERT(item);
-	ASSERT((keys > 0) && (keys <= PAGE_HT_KEYS));
-
+	ASSERT(keys > 0);
+	ASSERT(keys <= PAGE_HT_KEYS);
+	
 	/*
 	 * Convert item to PTE.
-	 */
-	t = hash_table_get_instance(item, pte_t, link);
-
-	if (keys == PAGE_HT_KEYS) {
-		return (key[KEY_AS] == (uintptr_t) t->as) &&
-		    (key[KEY_PAGE] == t->page);
-	} else {
-		return (key[KEY_AS] == (uintptr_t) t->as);
-	}
+	 *
+	 */
+	pte_t *pte = hash_table_get_instance(item, pte_t, link);
+	
+	if (keys == PAGE_HT_KEYS)
+		return (key[KEY_AS] == (uintptr_t) pte->as) &&
+		    (key[KEY_PAGE] == pte->page);
+	
+	return (key[KEY_AS] == (uintptr_t) pte->as);
 }
 
@@ -148,17 +151,17 @@
  *
  * @param item Page hash table item being removed.
+ *
  */
 void remove_callback(link_t *item)
 {
-	pte_t *t;
-
 	ASSERT(item);
-
+	
 	/*
 	 * Convert item to PTE.
-	 */
-	t = hash_table_get_instance(item, pte_t, link);
-
-	free(t);
+	 *
+	 */
+	pte_t *pte = hash_table_get_instance(item, pte_t, link);
+	
+	free(pte);
 }
 
@@ -166,16 +169,17 @@
  *
  * Map virtual address page to physical address frame
- * using flags. 
+ * using flags.
  *
  * The page table must be locked and interrupts must be disabled.
  *
- * @param as Address space to which page belongs.
- * @param page Virtual address of the page to be mapped.
+ * @param as    Address space to which page belongs.
+ * @param page  Virtual address of the page to be mapped.
  * @param frame Physical address of memory frame to which the mapping is done.
  * @param flags Flags to be used for mapping.
- */
-void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags)
-{
-	pte_t *t;
+ *
+ */
+void ht_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
+    unsigned int flags)
+{
 	unative_t key[2] = {
 		(uintptr_t) as,
@@ -184,21 +188,21 @@
 	
 	if (!hash_table_find(&page_ht, key)) {
-		t = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
-		ASSERT(t != NULL);
-
-		t->g = (flags & PAGE_GLOBAL) != 0;
-		t->x = (flags & PAGE_EXEC) != 0;
-		t->w = (flags & PAGE_WRITE) != 0;
-		t->k = !(flags & PAGE_USER);
-		t->c = (flags & PAGE_CACHEABLE) != 0;
-		t->p = !(flags & PAGE_NOT_PRESENT);
-		t->a = false;
-		t->d = false;
-
-		t->as = as;
-		t->page = ALIGN_DOWN(page, PAGE_SIZE);
-		t->frame = ALIGN_DOWN(frame, FRAME_SIZE);
-
-		hash_table_insert(&page_ht, key, &t->link);
+		pte_t *pte = (pte_t *) malloc(sizeof(pte_t), FRAME_ATOMIC);
+		ASSERT(pte != NULL);
+		
+		pte->g = (flags & PAGE_GLOBAL) != 0;
+		pte->x = (flags & PAGE_EXEC) != 0;
+		pte->w = (flags & PAGE_WRITE) != 0;
+		pte->k = !(flags & PAGE_USER);
+		pte->c = (flags & PAGE_CACHEABLE) != 0;
+		pte->p = !(flags & PAGE_NOT_PRESENT);
+		pte->a = false;
+		pte->d = false;
+		
+		pte->as = as;
+		pte->page = ALIGN_DOWN(page, PAGE_SIZE);
+		pte->frame = ALIGN_DOWN(frame, FRAME_SIZE);
+		
+		hash_table_insert(&page_ht, key, &pte->link);
 	}
 }
@@ -212,6 +216,7 @@
  * The page table must be locked and interrupts must be disabled.
  *
- * @param as Address space to wich page belongs.
+ * @param as   Address space to wich page belongs.
  * @param page Virtual address of the page to be demapped.
+ *
  */
 void ht_mapping_remove(as_t *as, uintptr_t page)
@@ -236,13 +241,12 @@
  * The page table must be locked and interrupts must be disabled.
  *
- * @param as Address space to wich page belongs.
+ * @param as   Address space to wich page belongs.
  * @param page Virtual page.
  *
  * @return NULL if there is no such mapping; requested mapping otherwise.
+ *
  */
 pte_t *ht_mapping_find(as_t *as, uintptr_t page)
 {
-	link_t *hlp;
-	pte_t *t = NULL;
 	unative_t key[2] = {
 		(uintptr_t) as,
@@ -250,9 +254,9 @@
 	};
 	
-	hlp = hash_table_find(&page_ht, key);
-	if (hlp)
-		t = hash_table_get_instance(hlp, pte_t, link);
-
-	return t;
+	link_t *cur = hash_table_find(&page_ht, key);
+	if (cur)
+		return hash_table_get_instance(cur, pte_t, link);
+	
+	return NULL;
 }
 
Index: kernel/genarch/src/mm/page_pt.c
===================================================================
--- kernel/genarch/src/mm/page_pt.c	(revision ba7371f9d732a62dea4383ffb0e509bccd9f4e4a)
+++ kernel/genarch/src/mm/page_pt.c	(revision da1bafb8cf9a3b3be8ef21bc114daaa476a85190)
@@ -33,5 +33,5 @@
 /**
  * @file
- * @brief	Virtual Address Translation for hierarchical 4-level page tables.
+ * @brief Virtual Address Translation for hierarchical 4-level page tables.
  */
 
@@ -46,7 +46,7 @@
 #include <memstr.h>
 
-static void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags);
-static void pt_mapping_remove(as_t *as, uintptr_t page);
-static pte_t *pt_mapping_find(as_t *as, uintptr_t page);
+static void pt_mapping_insert(as_t *, uintptr_t, uintptr_t, unsigned int);
+static void pt_mapping_remove(as_t *, uintptr_t);
+static pte_t *pt_mapping_find(as_t *, uintptr_t);
 
 page_mapping_operations_t pt_mapping_operations = {
@@ -63,43 +63,42 @@
  * The page table must be locked and interrupts must be disabled.
  *
- * @param as Address space to wich page belongs.
- * @param page Virtual address of the page to be mapped.
+ * @param as    Address space to wich page belongs.
+ * @param page  Virtual address of the page to be mapped.
  * @param frame Physical address of memory frame to which the mapping is done.
  * @param flags Flags to be used for mapping.
- */
-void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame, int flags)
+ *
+ */
+void pt_mapping_insert(as_t *as, uintptr_t page, uintptr_t frame,
+    unsigned int flags)
 {
-	pte_t *ptl0, *ptl1, *ptl2, *ptl3;
-	pte_t *newpt;
-
-	ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
-
+	pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
+	
 	if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT) {
-		newpt = (pte_t *)frame_alloc(PTL1_SIZE, FRAME_KA);
+		pte_t *newpt = (pte_t *) frame_alloc(PTL1_SIZE, FRAME_KA);
 		memsetb(newpt, FRAME_SIZE << PTL1_SIZE, 0);
 		SET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page), KA2PA(newpt));
 		SET_PTL1_FLAGS(ptl0, PTL0_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
 	}
-
-	ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
-
+	
+	pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
+	
 	if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT) {
-		newpt = (pte_t *)frame_alloc(PTL2_SIZE, FRAME_KA);
+		pte_t *newpt = (pte_t *) frame_alloc(PTL2_SIZE, FRAME_KA);
 		memsetb(newpt, FRAME_SIZE << PTL2_SIZE, 0);
 		SET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page), KA2PA(newpt));
 		SET_PTL2_FLAGS(ptl1, PTL1_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
 	}
-
-	ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
-
+	
+	pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
+	
 	if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT) {
-		newpt = (pte_t *)frame_alloc(PTL3_SIZE, FRAME_KA);
+		pte_t *newpt = (pte_t *) frame_alloc(PTL3_SIZE, FRAME_KA);
 		memsetb(newpt, FRAME_SIZE << PTL3_SIZE, 0);
 		SET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page), KA2PA(newpt));
 		SET_PTL3_FLAGS(ptl2, PTL2_INDEX(page), PAGE_PRESENT | PAGE_USER | PAGE_EXEC | PAGE_CACHEABLE | PAGE_WRITE);
 	}
-
-	ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
-
+	
+	pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
+	
 	SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(page), frame);
 	SET_FRAME_FLAGS(ptl3, PTL3_INDEX(page), flags);
@@ -116,42 +115,41 @@
  * The page table must be locked and interrupts must be disabled.
  *
- * @param as Address space to wich page belongs.
+ * @param as   Address space to wich page belongs.
  * @param page Virtual address of the page to be demapped.
+ *
  */
 void pt_mapping_remove(as_t *as, uintptr_t page)
 {
-	pte_t *ptl0, *ptl1, *ptl2, *ptl3;
-	bool empty = true;
-	int i;
-
 	/*
 	 * First, remove the mapping, if it exists.
+	 *
 	 */
-
-	ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
-
+	
+	pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
 	if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
 		return;
-
-	ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
-
+	
+	pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
 	if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
 		return;
-
-	ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
-
+	
+	pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
 	if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
 		return;
-
-	ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
-
+	
+	pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
+	
 	/* Destroy the mapping. Setting to PAGE_NOT_PRESENT is not sufficient. */
 	memsetb(&ptl3[PTL3_INDEX(page)], sizeof(pte_t), 0);
-
+	
 	/*
 	 * Second, free all empty tables along the way from PTL3 down to PTL0.
+	 *
 	 */
 	
-	/* check PTL3 */
+	/* Check PTL3 */
+	bool empty = true;
+	
+	unsigned int i;
 	for (i = 0; i < PTL3_ENTRIES; i++) {
 		if (PTE_VALID(&ptl3[i])) {
@@ -160,16 +158,19 @@
 		}
 	}
+	
 	if (empty) {
 		/*
 		 * PTL3 is empty.
 		 * Release the frame and remove PTL3 pointer from preceding table.
+		 *
 		 */
 		frame_free(KA2PA((uintptr_t) ptl3));
-		if (PTL2_ENTRIES)
-			memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
-		else if (PTL1_ENTRIES)
-			memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
-		else
-			memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
+#if (PTL2_ENTRIES != 0)
+		memsetb(&ptl2[PTL2_INDEX(page)], sizeof(pte_t), 0);
+#elif (PTL1_ENTRIES != 0)
+		memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
+#else
+		memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
+#endif
 	} else {
 		/*
@@ -177,55 +178,60 @@
 		 * Therefore, there must be a path from PTL0 to PTL3 and
 		 * thus nothing to free in higher levels.
-		 */
-		return;
-	}
-	
-	/* check PTL2, empty is still true */
-	if (PTL2_ENTRIES) {
-		for (i = 0; i < PTL2_ENTRIES; i++) {
-			if (PTE_VALID(&ptl2[i])) {
-				empty = false;
-				break;
-			}
+		 *
+		 */
+		return;
+	}
+	
+	/* Check PTL2, empty is still true */
+#if (PTL2_ENTRIES != 0)
+	for (i = 0; i < PTL2_ENTRIES; i++) {
+		if (PTE_VALID(&ptl2[i])) {
+			empty = false;
+			break;
 		}
-		if (empty) {
-			/*
-			 * PTL2 is empty.
-			 * Release the frame and remove PTL2 pointer from preceding table.
-			 */
-			frame_free(KA2PA((uintptr_t) ptl2));
-			if (PTL1_ENTRIES)
-				memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
-			else
-				memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
+	}
+	
+	if (empty) {
+		/*
+		 * PTL2 is empty.
+		 * Release the frame and remove PTL2 pointer from preceding table.
+		 *
+		 */
+		frame_free(KA2PA((uintptr_t) ptl2));
+#if (PTL1_ENTRIES != 0)
+		memsetb(&ptl1[PTL1_INDEX(page)], sizeof(pte_t), 0);
+#else
+		memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
+#endif
+	} else {
+		/*
+		 * PTL2 is not empty.
+		 * Therefore, there must be a path from PTL0 to PTL2 and
+		 * thus nothing to free in higher levels.
+		 *
+		 */
+		return;
+	}
+#endif /* PTL2_ENTRIES != 0 */
+	
+	/* check PTL1, empty is still true */
+#if (PTL1_ENTRIES != 0)
+	for (i = 0; i < PTL1_ENTRIES; i++) {
+		if (PTE_VALID(&ptl1[i])) {
+			empty = false;
+			break;
 		}
-		else {
-			/*
-			 * PTL2 is not empty.
-			 * Therefore, there must be a path from PTL0 to PTL2 and
-			 * thus nothing to free in higher levels.
-			 */
-			return;
-		}
-	}
-
-	/* check PTL1, empty is still true */
-	if (PTL1_ENTRIES) {
-		for (i = 0; i < PTL1_ENTRIES; i++) {
-			if (PTE_VALID(&ptl1[i])) {
-				empty = false;
-				break;
-			}
-		}
-		if (empty) {
-			/*
-			 * PTL1 is empty.
-			 * Release the frame and remove PTL1 pointer from preceding table.
-			 */
-			frame_free(KA2PA((uintptr_t) ptl1));
-			memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
-		}
-	}
-
+	}
+	
+	if (empty) {
+		/*
+		 * PTL1 is empty.
+		 * Release the frame and remove PTL1 pointer from preceding table.
+		 *
+		 */
+		frame_free(KA2PA((uintptr_t) ptl1));
+		memsetb(&ptl0[PTL0_INDEX(page)], sizeof(pte_t), 0);
+	}
+#endif /* PTL1_ENTRIES != 0 */
 }
 
@@ -236,30 +242,27 @@
  * The page table must be locked and interrupts must be disabled.
  *
- * @param as Address space to which page belongs.
+ * @param as   Address space to which page belongs.
  * @param page Virtual page.
  *
- * @return NULL if there is no such mapping; entry from PTL3 describing the mapping otherwise.
+ * @return NULL if there is no such mapping; entry from PTL3 describing
+ *         the mapping otherwise.
+ *
  */
 pte_t *pt_mapping_find(as_t *as, uintptr_t page)
 {
-	pte_t *ptl0, *ptl1, *ptl2, *ptl3;
-
-	ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
-
+	pte_t *ptl0 = (pte_t *) PA2KA((uintptr_t) as->genarch.page_table);
 	if (GET_PTL1_FLAGS(ptl0, PTL0_INDEX(page)) & PAGE_NOT_PRESENT)
 		return NULL;
-
-	ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
-
+	
+	pte_t *ptl1 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(ptl0, PTL0_INDEX(page)));
 	if (GET_PTL2_FLAGS(ptl1, PTL1_INDEX(page)) & PAGE_NOT_PRESENT)
 		return NULL;
-
-	ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
-
+	
+	pte_t *ptl2 = (pte_t *) PA2KA(GET_PTL2_ADDRESS(ptl1, PTL1_INDEX(page)));
 	if (GET_PTL3_FLAGS(ptl2, PTL2_INDEX(page)) & PAGE_NOT_PRESENT)
 		return NULL;
-
-	ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
-
+	
+	pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL3_ADDRESS(ptl2, PTL2_INDEX(page)));
+	
 	return &ptl3[PTL3_INDEX(page)];
 }
