Index: kernel/arch/amd64/src/userspace.c
===================================================================
--- kernel/arch/amd64/src/userspace.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/arch/amd64/src/userspace.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -55,5 +55,5 @@
 	asm volatile (
 		"pushq %[udata_des]\n"
-		"pushq %[stack_size]\n"
+		"pushq %[stack_top]\n"
 		"pushq %[ipl]\n"
 		"pushq %[utext_des]\n"
@@ -65,5 +65,6 @@
 		"iretq\n"
 		:: [udata_des] "i" (GDT_SELECTOR(UDATA_DES) | PL_USER),
-		   [stack_size] "r" (kernel_uarg->uspace_stack + STACK_SIZE),
+		   [stack_top] "r" ((uint8_t *) kernel_uarg->uspace_stack +
+		       kernel_uarg->uspace_stack_size),
 		   [ipl] "r" (ipl),
 		   [utext_des] "i" (GDT_SELECTOR(UTEXT_DES) | PL_USER),
@@ -74,6 +75,5 @@
 	
 	/* Unreachable */
-	while (1)
-		;
+	while (1);
 }
 
Index: kernel/arch/arm32/src/userspace.c
===================================================================
--- kernel/arch/arm32/src/userspace.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/arch/arm32/src/userspace.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -92,5 +92,6 @@
 
 	/* set user stack */
-	ustate.sp = ((uint32_t)kernel_uarg->uspace_stack) + STACK_SIZE;
+	ustate.sp = ((uint32_t) kernel_uarg->uspace_stack) +
+	    kernel_uarg->uspace_stack_size;
 
 	/* set where uspace execution starts */
Index: kernel/arch/ia32/src/userspace.c
===================================================================
--- kernel/arch/ia32/src/userspace.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/arch/ia32/src/userspace.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -63,5 +63,5 @@
 		
 		"pushl %[udata_des]\n"
-		"pushl %[stack_size]\n"
+		"pushl %[stack_top]\n"
 		"pushl %[ipl]\n"
 		"pushl %[utext_des]\n"
@@ -75,5 +75,6 @@
 		:
 		: [udata_des] "i" (GDT_SELECTOR(UDATA_DES) | PL_USER),
-		  [stack_size] "r" ((uint8_t *) kernel_uarg->uspace_stack + STACK_SIZE),
+		  [stack_top] "r" ((uint8_t *) kernel_uarg->uspace_stack +
+		      kernel_uarg->uspace_stack_size),
 		  [ipl] "r" (ipl),
 		  [utext_des] "i" (GDT_SELECTOR(UTEXT_DES) | PL_USER),
Index: kernel/arch/ia64/src/ia64.c
===================================================================
--- kernel/arch/ia64/src/ia64.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/arch/ia64/src/ia64.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -232,14 +232,16 @@
 	 *
 	 * When calculating stack addresses, mind the stack split between the
-	 * memory stack and the RSE stack. Each occuppies STACK_SIZE / 2 bytes.
+	 * memory stack and the RSE stack. Each occuppies
+	 * uspace_stack_size / 2 bytes.
 	 */
 	switch_to_userspace((uintptr_t) kernel_uarg->uspace_entry,
-	    ((uintptr_t) kernel_uarg->uspace_stack) + STACK_SIZE / 2 -
+	    ((uintptr_t) kernel_uarg->uspace_stack) +
+	    kernel_uarg->uspace_stack_size / 2 -
 	    ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT),
-	    ((uintptr_t) kernel_uarg->uspace_stack) + STACK_SIZE / 2,
+	    ((uintptr_t) kernel_uarg->uspace_stack) +
+	    kernel_uarg->uspace_stack_size / 2,
 	    (uintptr_t) kernel_uarg->uspace_uarg, psr.value, rsc.value);
-
-	while (1)
-		;
+	
+	while (1);
 }
 
Index: kernel/arch/mips32/src/mips32.c
===================================================================
--- kernel/arch/mips32/src/mips32.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/arch/mips32/src/mips32.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -211,5 +211,6 @@
 	    cp0_status_um_bit | cp0_status_ie_enabled_bit));
 	cp0_epc_write((uintptr_t) kernel_uarg->uspace_entry);
-	userspace_asm(((uintptr_t) kernel_uarg->uspace_stack + STACK_SIZE),
+	userspace_asm(((uintptr_t) kernel_uarg->uspace_stack +
+	    kernel_uarg->uspace_stack_size),
 	    (uintptr_t) kernel_uarg->uspace_uarg,
 	    (uintptr_t) kernel_uarg->uspace_entry);
Index: kernel/arch/mips64/src/mips64.c
===================================================================
--- kernel/arch/mips64/src/mips64.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/arch/mips64/src/mips64.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -188,5 +188,6 @@
 	    cp0_status_um_bit | cp0_status_ie_enabled_bit));
 	cp0_epc_write((uintptr_t) kernel_uarg->uspace_entry);
-	userspace_asm(((uintptr_t) kernel_uarg->uspace_stack + STACK_SIZE),
+	userspace_asm(((uintptr_t) kernel_uarg->uspace_stack +
+	    kernel_uarg->uspace_stack_size),
 	    (uintptr_t) kernel_uarg->uspace_uarg,
 	    (uintptr_t) kernel_uarg->uspace_entry);
Index: kernel/arch/ppc32/src/ppc32.c
===================================================================
--- kernel/arch/ppc32/src/ppc32.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/arch/ppc32/src/ppc32.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -269,5 +269,6 @@
 {
 	userspace_asm((uintptr_t) kernel_uarg->uspace_uarg,
-	    (uintptr_t) kernel_uarg->uspace_stack + STACK_SIZE - SP_DELTA,
+	    (uintptr_t) kernel_uarg->uspace_stack +
+	    kernel_uarg->uspace_stack_size - SP_DELTA,
 	    (uintptr_t) kernel_uarg->uspace_entry);
 	
Index: kernel/arch/sparc64/src/sun4u/sparc64.c
===================================================================
--- kernel/arch/sparc64/src/sun4u/sparc64.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/arch/sparc64/src/sun4u/sparc64.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -156,11 +156,11 @@
 	(void) interrupts_disable();
 	switch_to_userspace((uintptr_t) kernel_uarg->uspace_entry,
-	    ((uintptr_t) kernel_uarg->uspace_stack) + STACK_SIZE
-	    - (ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT) + STACK_BIAS),
+	    ((uintptr_t) kernel_uarg->uspace_stack) +
+	    kernel_uarg->uspace_stack_size -
+	    (ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT) + STACK_BIAS),
 	    (uintptr_t) kernel_uarg->uspace_uarg);
-
-	for (;;)
-		;
-	/* not reached */
+	
+	/* Not reached */
+	while (1);
 }
 
Index: kernel/arch/sparc64/src/sun4v/sparc64.c
===================================================================
--- kernel/arch/sparc64/src/sun4v/sparc64.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/arch/sparc64/src/sun4v/sparc64.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -154,11 +154,11 @@
 	(void) interrupts_disable();
 	switch_to_userspace((uintptr_t) kernel_uarg->uspace_entry,
-	    ((uintptr_t) kernel_uarg->uspace_stack) + STACK_SIZE
-	    - (ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT) + STACK_BIAS),
+	    ((uintptr_t) kernel_uarg->uspace_stack) +
+	    kernel_uarg->uspace_stack_size -
+	    (ALIGN_UP(STACK_ITEM_SIZE, STACK_ALIGNMENT) + STACK_BIAS),
 	    (uintptr_t) kernel_uarg->uspace_uarg);
-
-	for (;;)
-		;
-	/* not reached */
+	
+	/* Not reached */
+	while (1);
 }
 
Index: kernel/generic/src/main/uinit.c
===================================================================
--- kernel/generic/src/main/uinit.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/generic/src/main/uinit.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -56,6 +56,4 @@
 void uinit(void *arg)
 {
-	uspace_arg_t uarg;
-	
 	/*
 	 * So far, we don't have a use for joining userspace threads so we
@@ -72,13 +70,17 @@
 #endif
 	
-	uarg.uspace_entry = ((uspace_arg_t *) arg)->uspace_entry;
-	uarg.uspace_stack = ((uspace_arg_t *) arg)->uspace_stack;
-	uarg.uspace_uarg = ((uspace_arg_t *) arg)->uspace_uarg;
-	uarg.uspace_thread_function = NULL;
-	uarg.uspace_thread_arg = NULL;
+	uspace_arg_t *uarg = (uspace_arg_t *) arg;
+	uspace_arg_t local_uarg;
 	
-	free((uspace_arg_t *) arg);
+	local_uarg.uspace_entry = uarg->uspace_entry;
+	local_uarg.uspace_stack = uarg->uspace_stack;
+	local_uarg.uspace_stack_size = uarg->uspace_stack_size;
+	local_uarg.uspace_uarg = uarg->uspace_uarg;
+	local_uarg.uspace_thread_function = NULL;
+	local_uarg.uspace_thread_arg = NULL;
 	
-	userspace(&uarg);
+	free(uarg);
+	
+	userspace(&local_uarg);
 }
 
Index: kernel/generic/src/proc/program.c
===================================================================
--- kernel/generic/src/proc/program.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/generic/src/proc/program.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -71,13 +71,4 @@
 int program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *prg)
 {
-	uspace_arg_t *kernel_uarg;
-	
-	kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
-	kernel_uarg->uspace_entry = (void *) entry_addr;
-	kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
-	kernel_uarg->uspace_thread_function = NULL;
-	kernel_uarg->uspace_thread_arg = NULL;
-	kernel_uarg->uspace_uarg = NULL;
-	
 	prg->loader_status = EE_OK;
 	prg->task = task_create(as, name);
@@ -92,6 +83,18 @@
 	    AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
 	    STACK_SIZE, AS_AREA_ATTR_NONE, &anon_backend, NULL, &virt, 0);
-	if (!area)
+	if (!area) {
+		task_destroy(prg->task);
 		return ENOMEM;
+	}
+	
+	uspace_arg_t *kernel_uarg = (uspace_arg_t *)
+	    malloc(sizeof(uspace_arg_t), 0);
+	
+	kernel_uarg->uspace_entry = (void *) entry_addr;
+	kernel_uarg->uspace_stack = (void *) virt;
+	kernel_uarg->uspace_stack_size = STACK_SIZE;
+	kernel_uarg->uspace_thread_function = NULL;
+	kernel_uarg->uspace_thread_arg = NULL;
+	kernel_uarg->uspace_uarg = NULL;
 	
 	/*
@@ -100,6 +103,10 @@
 	prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
 	    THREAD_FLAG_USPACE, "uinit", false);
-	if (!prg->main_thread)
+	if (!prg->main_thread) {
+		free(kernel_uarg);
+		as_area_destroy(as, virt);
+		task_destroy(prg->task);
 		return ELIMIT;
+	}
 	
 	return EOK;
Index: kernel/generic/src/proc/thread.c
===================================================================
--- kernel/generic/src/proc/thread.c	(revision 2689a0ba890434ede8f74c746b6f5d89947db465)
+++ kernel/generic/src/proc/thread.c	(revision 3f57fb73696fea31ef6a619b390a707b4091eda9)
@@ -854,5 +854,4 @@
 	 * In case of failure, kernel_uarg will be deallocated in this function.
 	 * In case of success, kernel_uarg will be freed in uinit().
-	 *
 	 */
 	uspace_arg_t *kernel_uarg =
