diff --git a/src/server/httpd.c b/src/server/httpd.c
index 103c92f03d1ce118af6a54a7430cbc13d65d12a5..4b876d30065656b23174589279a5c35c649fc1ee 100644
--- a/src/server/httpd.c
+++ b/src/server/httpd.c
@@ -429,9 +429,14 @@ static int ahc_echo(void * cls, struct MHD_Connection * connection,
 }
 
 static struct MHD_Daemon * d;
+static pthread_mutex_t mutex;
+
 
 int httpd_start(void)
 {
+	pthread_mutexattr_t attr;
+	pthread_mutexattr_init( &attr );
+	pthread_mutex_init( &mutex, &attr );
 
 	int port = 8888;
 	LOG_USER("Launching httpd server on port %d", port);
@@ -461,15 +466,16 @@ int httpd_start(void)
 void httpd_stop(void)
 {
 	MHD_stop_daemon(d);
+	pthread_mutex_destroy( &mutex );
 }
 
 void openocd_sleep_prelude(void)
 {
-	/* FIX!!!! add locking here!!!! */
+	pthread_mutex_unlock( &mutex );
 }
 
 void openocd_sleep_postlude(void)
 {
-	/* FIX!!!! add locking here!!!! */
+	pthread_mutex_lock( &mutex );
 }
 
diff --git a/src/server/httpd/build.sh b/src/server/httpd/build.sh
index 93831a50e1b46e884e73bdfe159397a926e2308f..8824debf462a31c3a5682193261c9669ff95f4b4 100644
--- a/src/server/httpd/build.sh
+++ b/src/server/httpd/build.sh
@@ -1,3 +1,5 @@
 set e
 java -classpath ../../../../zy1000/build/xalan.jar\;. Stylizer menu.xsl menu.xml .
 find . -regex ".*\.tcl" -type f -exec sh html2tcl.sh {} {} \;
+echo "Copy .tcl files to /usr/local/lib/openocd/httpd/"
+cp *.tcl /usr/local/lib/openocd/httpd/
\ No newline at end of file
diff --git a/src/server/httpd/erase.tcl b/src/server/httpd/erase.tcl
index 3c29840d3a6784c0745e8da2b12358af4ee67119..fa9210736d6b12d42617c3139d99b2ca97487542 100644
--- a/src/server/httpd/erase.tcl
+++ b/src/server/httpd/erase.tcl
@@ -161,7 +161,7 @@ append buffer {
 				set form_length 0x10000
 			}  
 			if {[string compare $form_address ""]==0} {
-				if {[catch {[zy1000_flash]} result]==0} {
+				if {[catch {[first_flash_base]} result]==0} {
 						set form_address "0x[tohex $result]"
 					}			
 			}  
diff --git a/src/server/httpd/httpd.tcl b/src/server/httpd/httpd.tcl
index 607660cc82e0afbb45e120f52c0fc42e24a4b7d4..a8bce162860f3a252da8aa175e377cbc82c4ec2f 100644
--- a/src/server/httpd/httpd.tcl
+++ b/src/server/httpd/httpd.tcl
@@ -102,12 +102,12 @@ proc encode {a} {
 # catch any exceptions, capture output and return it 
 proc capture_catch {a} {
 	catch {
-		return [eval {capture $a}]
+		capture {uplevel $a}
 	} result
-	return $result
+	return $result 
 }
 
-proc zy1000_flash {} {
+proc first_flash_base {} {
 	set t [lindex 0 [ocd_flash_banks]]
 	return $t(base)
 }
diff --git a/src/server/httpd/menu.xml b/src/server/httpd/menu.xml
index 11daef94f3a7bbf5468f522fa03015f25a3d0ca7..26ecf7a91c5f7d697b3f8b236f984c25f922b41e 100644
--- a/src/server/httpd/menu.xml
+++ b/src/server/httpd/menu.xml
@@ -438,7 +438,7 @@
 				set form_length 0x10000
 			}  
 			if {[string compare $form_address ""]==0} {
-				if {[catch {[zy1000_flash]} result]==0} {
+				if {[catch {[first_flash_base]} result]==0} {
 						set form_address "0x[tohex $result]"
 					}			
 			}  
@@ -794,8 +794,8 @@ append console [encode [capture_catch poll]]
 				set form_command [formfetch form_command]
 
 				set form_edittext ""
-				if {[string length $form_command]>0} {				
-					catch {capture_catch {eval "$form_command"}} form_edittext
+				if {[string length $form_command]>0} {
+					set form_edittext [capture_catch {eval $form_command}]
 				}
 				
 				append buffer {<form action="openocd.tcl" method="post">} "\n"
diff --git a/src/server/httpd/openocd.tcl b/src/server/httpd/openocd.tcl
index cb28026e8dcd352ca464b3e0ce5b7d604543ffdd..4fed3877700b07a1243dc45847593881a7537486 100644
--- a/src/server/httpd/openocd.tcl
+++ b/src/server/httpd/openocd.tcl
@@ -154,8 +154,8 @@ append buffer {
 				set form_command [formfetch form_command]
 
 				set form_edittext ""
-				if {[string length $form_command]>0} {				
-					catch {capture_catch {eval "$form_command"}} form_edittext
+				if {[string length $form_command]>0} {
+					set form_edittext [capture_catch {eval $form_command}]
 				}
 				
 				append buffer {<form action="openocd.tcl" method="post">} "\n"