--- a/src/main.cc
+++ b/src/main.cc
@@ -23,6 +23,8 @@
 #include <signal.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <syslog.h>
+#include <stdlib.h>
 #include <sys/socket.h>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -31,9 +33,15 @@
 #include "socket.hh"
 #include "httpsock.hh"
 #include "version.hh"
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
 
 int numProc;
 
+const int ROOT_MAX_LEN = 500;
+char ROOT_DIR[ROOT_MAX_LEN] = WEBDIRPREFIX;
+
 void handleChildTerm( int )
 {
 	if( waitpid( 0, NULL, WNOHANG )>0 )
@@ -43,19 +51,14 @@
 	signal( SIGCHLD, handleChildTerm );
 }
 
-void spork()
+static void spork()
 {
 	struct passwd *pwent;
 	int user_id, group_id;
+
+	daemon(0,0);
 
-	chdir( "/" );
-
-	if( fork()!=0 )
-	{
-		exit( 0 );
-	}
-
-	setsid();
+	openlog("dhttpd",LOG_PID,LOG_DAEMON);
 
 	user_id = UID;
 	group_id = GID;
@@ -66,22 +69,27 @@
 		/* Check password file */
 		if ( (pwent = getpwuid( user_id ) ) == NULL)
 		{
+			syslog(LOG_ERR,"User with UID %d does not have an entry in /etc/passwd"
+				,user_id);
     			exit( 1 );
 		}
 
 		/* Reset groups attribute. */
 		if( initgroups( pwent->pw_name, group_id ) == -1 )
 		{
+			syslog(LOG_ERR,"Could not set up process groups (%m)");
 			exit( 1 );
 		}
 
 		/* Switch to our new user id.  We have to set the group first */
 		if( setgid( group_id )==-1 )
 		{
+			syslog(LOG_ERR,"Could not change GID to %d (%m)",group_id);
 			exit( 1 );
 		}
 		if( setuid( user_id )==-1 )
 		{
+			syslog(LOG_ERR,"Could not change UID to %d (%m)",user_id);
 			exit( 1 );
 		}
 	}
@@ -91,10 +99,12 @@
 {
 	int o;
 	int portnum = DEFAULTPORT;
+	char *bind_addr = DEFAULTBINDADDR;
+	bool nofork=false;
 
 	for( ;; )
 	{
-		o = getopt( argc, argv, "p:h" );
+		o = getopt( argc, argv, "p:b:hdr:" );
 		if( o==-1 )
 		{
 			break;
@@ -109,27 +119,59 @@
 			case 'h':
 				printf( "usage: %s [options]\n", argv[ 0 ] );
 				printf( "  -p (port)  Use a different port than the default of %i\n", DEFAULTPORT );
+				printf( "  -b         Bind to this address instead of %s\n", DEFAULTBINDADDR);
 				printf( "  -h         Help\n" );
+				printf( "  -d         Do not fork into Background on startup\n" );
 				return 0;
 
 			case 'p':
 				sscanf( optarg, "%i", &portnum );
 				break;
+			case 'b':
+				bind_addr = optarg;
+				break;
+			case 'r':
+			        if ( strlen(optarg) > ROOT_MAX_LEN )
+			        {
+				    //  Prevent overflows
+				    fprintf(stderr,
+					    "[ERROR] Too long WWW root path: %s\n"
+					    , optarg );
+
+				    exit(1);
+			        }
+
+			        if ( optarg[strlen(optarg) -1] == '/' )
+			        {
+				    fprintf(stderr,
+					    "[ERROR] No trailing slash allowed: %s\n"
+					    , optarg );
+				    exit(1);
+				}
+
+				sscanf( optarg, "%s", ROOT_DIR );
+				break;
+			case 'd':
+				nofork=true;
+				break;
 		}
 	}
 
-	ListenSocket listen( portnum );
+	ListenSocket listen_socket( portnum, bind_addr );
 	pid_t pid;
 	int s;
 
-	if( listen.geterror() )
+	if( listen_socket.geterror() )
 	{
-		fprintf( stderr, "Could not open port %i.  %s failed to start\n", portnum, DHTTPDVERSION );
+		fprintf( stderr, "Could not listen on port %i.  %s failed to start\n", portnum, DHTTPDVERSION );
 		exit( 1 );
 	}
 
-	/* now let's become a daemon */
-	spork();
+	if(!nofork)
+	{
+		/* now let's become a daemon */
+		spork();
+	}
 
 	signal( SIGCHLD, handleChildTerm );
 	signal( SIGHUP, SIG_IGN );
@@ -139,7 +181,7 @@
 	{
 		if( numProc<MAXCHILDPROC )
 		{
-			s = listen.newsock();
+			s = listen_socket.newsock();
 		}
 		else
 		{
@@ -154,7 +196,7 @@
 			if( pid==0 )
 			{
 				/* Child process (fulfill request) */
-				close( listen.getfd() );
+				close( listen_socket.getfd() );
 				
 				/* Handle Socket */ {
 					HttpSocket sock( s );
@@ -178,6 +220,9 @@
 				close( s );
 			}
 		}
+		/* In case signals are lost */
+		while( waitpid(0, NULL, WNOHANG)>0 )
+			numProc--;
 	}
 
 	/* make compiler happy */
