diff --git a/bin/foamUpdateCaseFileHeader b/bin/foamUpdateCaseFileHeader
index a90510765373ffae07de59eccda40e503a99095f..37d4e6d4981d02ad334ef45b87304c23946b0d22 100755
--- a/bin/foamUpdateCaseFileHeader
+++ b/bin/foamUpdateCaseFileHeader
@@ -28,8 +28,8 @@
 # Description
 #     Updates the header of application files.
 #     By default, writes current version in the header.
-#     Alternatively version can be specified with -v option.
-#     Also removes consecutive blank lines from file.
+#     The version can alternatively be specified with the -v option.
+#     Also removes consecutive blank lines from the file.
 #
 #------------------------------------------------------------------------------
 usage() {
@@ -66,7 +66,7 @@ do
         shift 2
         ;;
     -*)
-        usage "unknown option: '$*'"
+        usage "unknown option: '$1'"
         ;;
     *)
         break
@@ -118,7 +118,7 @@ FoamFileAttribute()
 tmpFile=FoamFile.tmp$$
 for caseFile
 do
-    if grep FoamFile $caseFile >/dev/null 2>&1
+    if grep -q FoamFile $caseFile 2>/dev/null
     then
         echo "Updating case file: $caseFile"
         sed -n '/FoamFile/,/}/p' $caseFile > $tmpFile
@@ -137,7 +137,7 @@ do
         [ -s $tmpFile ] && cat $tmpFile >| $caseFile
         rm -f $tmpFile 2>/dev/null
     else
-        echo " Invalid case file: $caseFile" 1>&2
+        echo "Invalid case file: $caseFile" 1>&2
     fi
 done
 
diff --git a/wmake/scripts/wmakeFunctions b/wmake/scripts/wmakeFunctions
index 7a12ad05459ecc6a5e5de9826464960c8f153104..4ed0b7b0f38cff2d9c924988d7a8ddfa665e7a38 100644
--- a/wmake/scripts/wmakeFunctions
+++ b/wmake/scripts/wmakeFunctions
@@ -48,6 +48,12 @@ checkEnv()
 # Search up directories tree for the Make sub-directory
 #------------------------------------------------------------------------------
 
+# Return the absolute path for a directory or a file's parent directory
+# expandPath dirName
+# expandPath fileName
+#
+# Sets:
+# - exPath
 expandPath()
 {
     if [ -d "$1" ]
@@ -58,6 +64,19 @@ expandPath()
     fi
 }
 
+# Find the target directory
+# findTarget dirName
+# findTarget fileName
+#
+# Sets:
+# - dir
+#
+# Side-effect variables:
+# - sets exPath
+# - sets wmpdir
+#
+# Global variables used:
+# - WM_PROJECT_DIR, HOME
 findTarget()
 {
     expandPath $WM_PROJECT_DIR
@@ -65,28 +84,38 @@ findTarget()
     expandPath $1
 
     if [ "$exPath" = "$wmpdir" \
-                   -o "$exPath" = "$HOME" \
-                   -o "$exPath" = "/" \
-       ]
+      -o "$exPath" = "$HOME" \
+      -o "$exPath" = "/" \
+    ]
     then
         echo "$Script error: could not find Make directory" 1>&2
         exit 1
-    elif [ -d "$1/Make" ]; then
+    elif [ -d "$1/Make" ]
+    then
         dir=$1
     else
         findTarget "$1/.."
     fi
 }
 
+# Change to 'MakeDir'
+# - 'MakeDir' for its input
+#
+# Sets:
+# - dir
+#
+# Side-effect variables:
+# - sets exPath
+# - unsets targetType
 cdSource()
 {
     if [ ! -d $MakeDir ]
     then
         echo "$Script: '$MakeDir' directory does not exist in $PWD" 1>&2
-        echo "    Searching up directories tree for Make directory"
+        echo "    Searching up directories tree for Make directory" 1>&2
+        unset targetType
 
         findTarget .
-        targetType=
 
         if [ "$dir" ]
         then
@@ -97,13 +126,30 @@ cdSource()
         fi
     fi
 
-
     [ -r $MakeDir/files ] || {
         echo "$Script error: file '$MakeDir/files' does not exist in $PWD" 1>&2
         exit 1
     }
 }
 
+
+# Find the object directory
+# findObjectDir dirName
+# findObjectDir fileName
+#
+# Sets:
+# - dir
+# - path
+# - appDir
+# - objectsDir
+#
+# Side-effect variables:
+# - sets exPath
+# - sets wmpdir
+# - set platformPath
+#
+# Global variables used:
+# - WM_PROJECT_DIR, WM_OPTIONS
 findObjectDir()
 {
     expandPath $WM_PROJECT_DIR
@@ -127,7 +173,18 @@ findObjectDir()
     fi
 }
 
-if [ -n "$BASH_VERSION" ]; then
+
+# depToSource
+# - uses 'depFile' for its input
+#
+# Sets:
+# - sourceFile
+#
+# Global variables used:
+# - WM_OPTIONS
+# - WM_MPLIB
+if [ -n "$BASH_VERSION" ]
+then
     depToSource()
     {
         sourceFile=${depFile%.dep}
diff --git a/wmake/wclean b/wmake/wclean
index de9bd6c58db73b9fcb41ed3babb91a0aa9465df3..42209bc2bb8ce5cee4c18ebd3f074f531f3095a0 100755
--- a/wmake/wclean
+++ b/wmake/wclean
@@ -4,7 +4,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
-#    \\/     M anipulation  |
+#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 #-------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM.
@@ -34,7 +34,7 @@
 #     lnInclude directories generated for libraries.
 #
 #------------------------------------------------------------------------------
-Script=${0##*/}
+Script=${0##*/}     # Note: need 'Script' for some functions in wmakeFunctions
 
 # Source the wmake functions
 . ${0%/*}/scripts/wmakeFunctions
@@ -82,7 +82,7 @@ do
         shift
         ;;
     -*)
-        usage "unknown option: '$*'"
+        usage "unknown option: '$1'"
         ;;
     *)
         break
@@ -117,7 +117,7 @@ then
     # Specified alternative name for the Make sub-directory:
     [ $# -ge 3 ] && MakeDir=$3
 
-    if [ "$dir" ]
+    if [ -n "$dir" ]
     then
         cd $dir 2>/dev/null || {
             echo "$Script error: could not change to directory '$dir'" 1>&2
@@ -126,8 +126,7 @@ then
     fi
 
     # Print command
-    [ -z "$targetType" ] || targetSpace=" "
-    echo "$Script $targetType$targetSpace${dir:-.}"
+    echo "$Script $targetType${targetType:+ }${dir:-.}"
 fi
 
 
@@ -147,7 +146,6 @@ fi
 if [ "$targetType" = empty ]
 then
     # First pass: clean up empty source code directories
-
     echo "Removing empty directories..."
 
     # Get sub-directories avoiding particular directories
@@ -259,8 +257,8 @@ then
     objectsDir=$MakeDir/$WM_OPTIONS
     if [ $(echo $PWD | grep "$WM_PROJECT_DIR") ]
     then
-        platformPath=$WM_PROJECT_DIR/platforms/${WM_OPTIONS}
-        objectsDir=$platformPath$(echo $PWD | sed s%$WM_PROJECT_DIR%% )
+        buildPath=$WM_PROJECT_DIR/platforms/${WM_OPTIONS}
+        objectsDir=$buildPath$(echo $PWD | sed s%$WM_PROJECT_DIR%%)
     fi
     rm -rf $objectsDir 2>/dev/null
 fi
@@ -270,17 +268,8 @@ fi
 # Remove the lnInclude directory if present
 #------------------------------------------------------------------------------
 
-if [ -d lnInclude ]
-then
-    rm -rf lnInclude 2>/dev/null
-fi
-
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage MakeDir
+[ -d lnInclude ] && rm -rf lnInclude 2>/dev/null
 
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wcleanLnIncludeAll b/wmake/wcleanLnIncludeAll
index 2a4a434485ad5fb0ff981af8fd11c424d8bf4bf8..4ac19b6816b0a61d9f152d02c3cabc68a8d7a880 100755
--- a/wmake/wcleanLnIncludeAll
+++ b/wmake/wcleanLnIncludeAll
@@ -26,45 +26,28 @@
 #     wcleanLnIncludeAll
 #
 # Usage
-#     wcleanLnIncludeAll [dir1] .. [dirN]
+#     wcleanLnIncludeAll [dir1 [..dirN]]
 #
 # Description
 #     Delete all the lnInclude directories in the tree.
 #
-##------------------------------------------------------------------------------
-Script=${0##*/}
-
-# Source the wmake functions
-. ${0%/*}/scripts/wmakeFunctions
-
-#------------------------------------------------------------------------------
-# Parse arguments and options
 #------------------------------------------------------------------------------
 
-# Default to the CWD
 if [ "$#" -eq 0 ]
 then
-    # Add the CWD to the arguments
+    # Default is to search the CWD
     set -- .
 elif [ "$1" = "-h" -o "$1" = "-help" ]
 then
-    echo "Usage: $Script [dir1] .. [dirN]"
-    echo
-    echo "    Delete all the lnInclude directories in the tree"
-    echo
-    exit 1
-fi
-
-
-#------------------------------------------------------------------------------
-# Search up the directory tree for the Make sub-directory,
-# check the existance of the 'files' file and clean there if present
-#------------------------------------------------------------------------------
+    exec 1>&2
+    cat<<USAGE
+Usage: ${0##*/} [dir1 [..dirN]]
 
-# Need to add an option or special logic to control cdSource
-# MakeDir=Make
-# cdSource
+* Remove all lnInclude directories found in the tree
 
+USAGE
+    exit 1
+fi
 
 #------------------------------------------------------------------------------
 # Search for all lnInclude directories and delete them
@@ -81,12 +64,6 @@ do
     fi
 done
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script MakeDir
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wcleanPlatform b/wmake/wcleanPlatform
index a9d20ceea9638455538dc979a3fda8f404f9865d..36605bdf5522f82e64c0d41d0a2484cf3df5e8a1 100755
--- a/wmake/wcleanPlatform
+++ b/wmake/wcleanPlatform
@@ -4,7 +4,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2015 OpenFOAM Foundation
-#    \\/     M anipulation  |
+#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 #-------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM.
@@ -71,7 +71,8 @@ USAGE
 }
 
 # Print help message
-if [ "$1" = "-h" -o "$1" = "-help" ]; then
+if [ "$1" = "-h" -o "$1" = "-help" ]
+then
     usage
 fi
 
@@ -79,15 +80,11 @@ fi
 [ "$PWD" = "$WM_PROJECT_DIR" ] || \
     usage "Not in the project top-level directory " $WM_PROJECT_DIR
 
-# Get the platforms from the arguments
-platforms="$@"
+# Get the platforms from the arguments or '-current'
+platforms="${@:--current}"
 
-# If no arguments are provided select the current platform
-if [ "$#" -lt 1 ]; then
-    platforms="-current"
-fi
-
-if [ "$platforms" = "-all" ]; then
+if [ "$platforms" = "-all" ]
+then
     echo "Removing all platforms/sub-directories"
     rm -rf platforms/*
 
@@ -116,12 +113,6 @@ else
     done
 fi
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script platforms
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wdep b/wmake/wdep
index 916ef72cb28a69a5d68e828183a6cb014b35c44d..dd20f21a97ab67353659211905af14c54ba068a7 100755
--- a/wmake/wdep
+++ b/wmake/wdep
@@ -74,18 +74,23 @@ do
     esac
 done
 
+if [ $# -eq 0 ]
+then
+    echo "$Script: no source file specified" 1>&2
+    exit 1
+fi
+
 # Check environment variables
 checkEnv
 
+sourceFile=$1
 
 #------------------------------------------------------------------------------
-# Check <file> is is the current directory,
+# Check <file> is in the current directory,
 # otherwise search tree for first occurrance
 #------------------------------------------------------------------------------
 
-sourceFile=$1
-
-if [ ! -e $1 ]
+if [ ! -e "$sourceFile" ]
 then
     sourceFile=$(find . -name $sourceFile -print -quit)
     if [ -z "$sourceFile" ]
@@ -95,24 +100,14 @@ then
     fi
 fi
 
-
 #------------------------------------------------------------------------------
 # Search up directories tree for the Make sub-directory containing dep files
 # and echo path for the dep file corresponding to the specified source file
 #------------------------------------------------------------------------------
 
 findObjectDir $sourceFile
+echo "$objectsDir/${sourceFile##*/}.dep"
 
-fileName=${1##*/}
-
-echo $objectsDir/$fileName.dep
-
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wmake b/wmake/wmake
index 1c3ad99fa6f397048d2fe4abd6c24d38461ba418..e251b1e9f404e3cbf1c90e7682203970be9ed01d 100755
--- a/wmake/wmake
+++ b/wmake/wmake
@@ -53,7 +53,7 @@
 #     wclean, wcleanPlatform, wcleanLnIncludeAll
 #
 #------------------------------------------------------------------------------
-Script=${0##*/}
+Script=${0##*/}     # Note: need 'Script' for some functions in wmakeFunctions
 
 # Source the wmake functions
 . ${0%/*}/scripts/wmakeFunctions
@@ -127,8 +127,7 @@ useAllCores()
 #------------------------------------------------------------------------------
 
 # Default to compiling the local target only
-all=
-update=
+unset all update
 
 while [ "$#" -gt 0 ]
 do
@@ -141,10 +140,10 @@ do
             export WM_QUIET=1
             ;;
         -a | -all | all)
-            all="all"
+            all=all
             ;;
         -q | -queue | queue)
-            all="queue"
+            all=queue
             ;;
         # Parallel compilation on all cores of local machine
         -j)
@@ -154,13 +153,13 @@ do
             echo "Compiling enabled on $WM_NCOMPPROCS cores"
             ;;
         # Parallel compilation on specified number of cores
-        -j*)
+        -j[1-9]*)
             export WM_NCOMPPROCS=${1#-j}
             echo "Compiling enabled on $WM_NCOMPPROCS cores"
             ;;
         # Non-stop compilation, ignoring errors
         -k | -non-stop)
-            export WM_CONTINUE_ON_ERROR=1
+            export WM_CONTINUE_ON_ERROR=true
             ;;
         # Disable scheduled parallel compilation
         -no-scheduler)
@@ -173,15 +172,15 @@ do
         # - remove empty directories, along with deprecated object directories
         #   and respective binaries.
         -update)
-            update="true"
-            [ -z "$all" ] && all="all"
+            update=true
+            : ${all:=all}  # implies 'all', unless previous set to 'queue' etc.
             ;;
         --)
             shift
             break
             ;;
         -*)
-            usage "unknown option: '$*'"
+            usage "unknown option: '$1'"
             ;;
         *)
             break
@@ -200,10 +199,10 @@ checkEnv
 # When compiling anything but a standalone exe WM_PROJECT and WM_PROJECT_DIR
 # must be set
 [ "$1" = exe -o \( "$WM_PROJECT" -a "$WM_PROJECT_DIR" \) ] || {
-    echo "$Script error:" 1>&2
-    echo "    environment variable \$WM_PROJECT or " \
-         "\$WM_PROJECT_DIR not set" 1>&2
-    echo "    while building project library" 1>&2
+    exec 1>&2
+    echo "$Script error:"
+    echo "    environment variable \$WM_PROJECT or \$WM_PROJECT_DIR not set"
+    echo "    while building project library"
     exit 1
 }
 
@@ -219,11 +218,11 @@ then
     [ $? -eq 0 ] || unset WM_NCOMPPROCS
 fi
 
-if [ "$WM_NCOMPPROCS" ]
+if [ -n "$WM_NCOMPPROCS" ]
 then
     parOpt="-j $WM_NCOMPPROCS"
 
-    if [ "$WM_NCOMPPROCS" -gt 1 -a ! "$MAKEFLAGS" ]
+    if [ "$WM_NCOMPPROCS" -gt 1 -a -z "$MAKEFLAGS" ]
     then
         lockDir=$HOME/.$WM_PROJECT/.wmake
 
@@ -265,7 +264,7 @@ then
     # Specified alternative name for the Make sub-directory:
     [ $# -ge 3 ] && MakeDir=$3
 
-    if [ "$dir" ]
+    if [ -n "$dir" ]
     then
         cd $dir 2>/dev/null || {
             echo "$Script error: could not change to directory '$dir'" 1>&2
@@ -274,16 +273,15 @@ then
     fi
 
     # Print command
-    [ -z "$targetType" ] || targetSpace=" "
-    echo "$Script $targetType$targetSpace${dir:-.}"
+    echo "$Script $targetType${targetType:+ }${dir:-.}"
 fi
 
 
 #------------------------------------------------------------------------------
-# Recurse the source tree to compile "all" targets
+# Recurse the source tree to update all
 #------------------------------------------------------------------------------
 
-if [ -n "$update" ]
+if [ "$update" = true ]
 then
     wrmdep -update
     wrmdep -old
@@ -297,40 +295,37 @@ fi
 # Recurse the source tree to compile "all" targets
 #------------------------------------------------------------------------------
 
-if [ "$all" = "all" ]
+if [ "$all" = all ]
 then
     if [ -e Allwmake ]
     then
         ./Allwmake -fromWmake $targetType
         exit $?
+    fi
+
+    # Find all the sub-directories containing a 'Make' directory
+    # (xargs is just used to flatten the list)
+    FOAM_APPS=$(
+        for d in *
+        do [ -d "$d" -a "$d" != Optional -a "$d" != Make ] && echo "$d"
+        done | xargs)
+
+    if [ -n "$FOAM_APPS" ]
+    then
+        # Compile all applications in sub-directories
+        $make ${WM_CONTINUE_ON_ERROR:+-k} \
+              -f $WM_DIR/makefiles/apps \
+              TARGET="$targetType" FOAM_APPS="$FOAM_APPS"
+        makeExitCode=$?
     else
-        # Have to keep track of the main exit code for the call to "make"
-        makeExitCode=0
-
-        # Find all the sub-directories containing a 'Make' directory
-        FOAM_APPS=$(\
-                       for d in *; \
-                       do [ -d "$d" -a "$d" != Optional -a "$d" != Make ] \
-                          && echo "$d"; \
-                       done | xargs \
-                 )
-        if [ ! "$FOAM_APPS" = "" ]
-        then
-            # Compile all applications in sub-directories
-            $make ${WM_CONTINUE_ON_ERROR:+-k} \
-                  -f $WM_DIR/makefiles/apps \
-                  TARGET="$targetType" FOAM_APPS="$FOAM_APPS"
-            makeExitCode=$?
-        fi
-        # If the current directory contains a 'Make' directory continue
-        # otherwise exit, or always exit in case of error
-        if [ ! -d $MakeDir -o $makeExitCode -ne 0 ]
-        then
-            exit $makeExitCode
-        fi
+        makeExitCode=0 # For fall-through
+    fi
 
-        # Clean up tracking variable
-        unset makeExitCode
+    # Exit if current directory does not contains a 'Make' directory or
+    # an error was previously encountered
+    if [ ! -d "$MakeDir" -o $makeExitCode -ne 0 ]
+    then
+        exit $makeExitCode
     fi
 fi
 
@@ -339,9 +334,9 @@ fi
 # Recurse the source tree to compile "all" targets using wmakeQueue
 #------------------------------------------------------------------------------
 
-if [ "$all" = "queue" ]
+if [ "$all" = queue ]
 then
-    [ -n "$update" ] || wmakeLnIncludeAll $parOpt
+    [ "$update" = true ] || wmakeLnIncludeAll $parOpt
 
     (
         export WM_COLLECT_DIR=$WM_PROJECT_DIR/platforms/${WM_OPTIONS}/${PWD////_}
@@ -369,20 +364,18 @@ cdSource
 
 # Transform no option to "libso" if that looks appropriate or remove it
 # so that the call to make builds the application
-if [ "$targetType" = "" ]
+if [ -z "$targetType" ]
 then
-    unset targetType
-    if grep -e '^ *LIB *=' "$MakeDir/files" >/dev/null 2>&1
+    if grep -qe '^ *LIB *=' "$MakeDir/files" 2>/dev/null
     then
         targetType=libso
     fi
-elif grep -e '^ *EXE *=' "$MakeDir/files" >/dev/null 2>&1
+elif grep -qe '^ *EXE *=' "$MakeDir/files" 2>/dev/null
 then
     # Application. Remove any nonsense targetType
     case "$targetType" in
-      lib*)
+    lib*)
         unset targetType
-        break
         ;;
     esac
 fi
@@ -396,8 +389,8 @@ fi
 objectsDir=$MakeDir/$WM_OPTIONS
 if [ $(echo $PWD | grep "$WM_PROJECT_DIR") ]
 then
-    platformPath=$WM_PROJECT_DIR/platforms/${WM_OPTIONS}
-    objectsDir=$platformPath$(echo $PWD | sed s%$WM_PROJECT_DIR%% )
+    buildPath=$WM_PROJECT_DIR/platforms/${WM_OPTIONS}
+    objectsDir=$buildPath$(echo $PWD | sed s%$WM_PROJECT_DIR%%)
 fi
 
 (
@@ -429,16 +422,15 @@ fi
 # Make the dependency files
 #------------------------------------------------------------------------------
 
-# For libraries create lnInclude ...
+# For libraries create lnInclude, but only if 'LIB' is declared in 'Make/files'
 case "$targetType" in
-    lib | libo | libso | dep )
-        # ... but only if 'LIB' is declared in 'Make/files'
-        if grep -e '^ *LIB *=' "$MakeDir/files" >/dev/null 2>&1
-        then
-            $make -s -f $WM_DIR/makefiles/general MAKE_DIR=$MakeDir \
-                  OBJECTS_DIR=$objectsDir lnInclude
-        fi
-        ;;
+(lib | libo | libso | dep)
+    if grep -qe '^ *LIB *=' "$MakeDir/files" 2>/dev/null
+    then
+        $make -s -f $WM_DIR/makefiles/general MAKE_DIR=$MakeDir \
+              OBJECTS_DIR=$objectsDir lnInclude
+    fi
+    ;;
 esac
 
 
@@ -453,12 +445,7 @@ then
         OBJECTS_DIR=$objectsDir updatedep
     makeExitCode=$?
 
-    if [ $makeExitCode -ne 0 ]
-    then
-        exit $makeExitCode
-    fi
-
-    unset makeExitCode
+    [ $makeExitCode -eq 0 ] || exit $makeExitCode
 fi
 
 
@@ -469,12 +456,6 @@ fi
 exec $make -f $WM_DIR/makefiles/general MAKE_DIR=$MakeDir \
      OBJECTS_DIR=$objectsDir $targetType
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage useAllCores update expandPath findTarget
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wmakeCheckPwd b/wmake/wmakeCheckPwd
index f10b6d2339cf08501c7e97ba9ca1c02b188a4b22..d21318778b11ed2e297f92853cdbf9e7c3f97a85 100755
--- a/wmake/wmakeCheckPwd
+++ b/wmake/wmakeCheckPwd
@@ -89,7 +89,7 @@ dirName="$1"
 # Simple check against $PWD
 [ "$PWD" = "$dirName" ] && exit 0
 
-# Check existance of <dir>
+# Check existence of <dir>
 [ -d "$dirName" ] || {
     [ "$quietOpt" = true ] || {
         echo "$Script error: Directory does not exist $dirName"
@@ -108,14 +108,7 @@ target=$(cd $dirName 2>/dev/null && /bin/pwd)
 [ "$quietOpt" = true ] || {
     echo "$Script error: Current directory is not $dirName"
 }
-exit 1
-
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage
 
+exit 1
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wmakeCollect b/wmake/wmakeCollect
index 2aaa2c6eef5fb531961c7de42238481bdddec7fe..014256df5219420194696eac5b2440b02bee7b73 100755
--- a/wmake/wmakeCollect
+++ b/wmake/wmakeCollect
@@ -68,8 +68,8 @@ USAGE
     exit 1
 }
 
-# Set true to clean-up file if interupted
-cleanup=
+# Set true to clean-up file if interrupted
+unset cleanup
 
 while [ "$#" -gt 0 ]
 do
@@ -78,7 +78,7 @@ do
         usage
         ;;
     -kill | -clean)
-        cleanup="true"
+        cleanup=true
         shift
         ;;
     -*)
@@ -110,7 +110,7 @@ makefile="$WM_COLLECT_DIR.Makefile"
 
 
 # Clean-up files and exit
-if [ -n "$cleanup" ]
+if [ "$cleanup" = true ]
 then
     rm -rf $WM_COLLECT_DIR
     rm -f $makefile
@@ -142,31 +142,23 @@ then
     echo -e "\t$E cd $PWD && \\" >> $file
     echo -e "\t${@:1:($#-1)} $object" >> $file
     echo >> $file
-else
-    if [ -d $WM_COLLECT_DIR ]
-    then
-        # Collect all the makefiles into a single makefiles for this build
-        (cd $WM_COLLECT_DIR && ls -1rt | xargs cat > $makefile)
+elif [ -d $WM_COLLECT_DIR ]
+then
+    # Collect all the makefiles into a single makefiles for this build
+    (cd $WM_COLLECT_DIR && ls -1rt | xargs cat > $makefile)
 
-        # Add a build rule for all of the targets
-        echo 'all: $(OBJECTS)' >> $makefile
+    # Add a build rule for all of the targets
+    echo 'all: $(OBJECTS)' >> $makefile
 
-        # Clear out all of the target makefiles
-        rm -rf $WM_COLLECT_DIR
+    # Clear out all of the target makefiles
+    rm -rf $WM_COLLECT_DIR
 
-        # Run make on the collected makefile
-        make -j $WM_NCOMPPROCS -f $makefile all
+    # Run make on the collected makefile
+    make -j $WM_NCOMPPROCS -f $makefile all
 
-        rm -f $makefile
-    fi
+    rm -f $makefile
 fi
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wmakeFilesAndOptions b/wmake/wmakeFilesAndOptions
index 488c211ab4e5ba93725b6adb3388d4dc36e15ee0..8519006ef0134030601a75696fae43e2b93b5074 100755
--- a/wmake/wmakeFilesAndOptions
+++ b/wmake/wmakeFilesAndOptions
@@ -60,7 +60,7 @@ do
         usage
         ;;
     -*)
-        usage "unknown option: '$*'"
+        usage "unknown option: '$1'"
         ;;
     *)
         break
@@ -102,11 +102,6 @@ fi
 }
 
 
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wmakeLnInclude b/wmake/wmakeLnInclude
index 3db3802de4cb15945533348d1d7c8ebf5a1bcb9d..0b338ac7429a7f92b41e7e4fa8e929e50173d267 100755
--- a/wmake/wmakeLnInclude
+++ b/wmake/wmakeLnInclude
@@ -91,7 +91,7 @@ do
             shift
             ;;
         -*)
-            usage "unknown option: '$*'"
+            usage "unknown option: '$1'"
             ;;
         *)
             break
@@ -167,12 +167,6 @@ find .. $findOpt \
      \)  \
      -exec ln $lnOpt {} . \;
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wmakeLnIncludeAll b/wmake/wmakeLnIncludeAll
index 73cd5798cf4f83844b2c1511e9e1ac8f356816f6..03a8930963c1ec64d3757b0417287557b0f3baa0 100755
--- a/wmake/wmakeLnIncludeAll
+++ b/wmake/wmakeLnIncludeAll
@@ -62,12 +62,12 @@ findName=lnInclude
 nCores=0
 
 # Default 'wmakeLnInclude' option
-wmLnOpt=
+unset wmLnOpt
 
 while [ "$#" -gt 0 ]
 do
     case "$1" in
-        -h | -help)   # Provide immediate help
+        -h | -help)
             usage
             ;;
         -u | -update)
@@ -75,16 +75,16 @@ do
             ;;
         # Parallel execution on WM_NCOMPPROCS cores
         -j)
-            nCores=$WM_NCOMPPROCS
+            nCores=${WM_NCOMPPROCS:-0}
             test $# -ge 2 && expr $2 + 1 > /dev/null 2>&1 \
                 && shift && nCores=$1
             ;;
         # Parallel compilation on specified number of cores
-        -j*)
+        -j[1-9]*)
             nCores=${1#-j}
             ;;
         -*)
-            usage "unknown option: '$*'"
+            usage "unknown option: '$1'"
             ;;
         *)
             break
@@ -93,9 +93,6 @@ do
     shift
 done
 
-
-FAIL=0
-
 if [ "$nCores" -gt 0 ]
 then
     echo "$Script: starting wmakeLnInclude processes on $nCores cores"
@@ -122,7 +119,7 @@ do
         topDir=${MakeDir%/Make}    # trim /Make from the end
         if [ -d "$topDir" ]
         then
-            if grep -e '^ *LIB *=' "$MakeDir/files" >/dev/null 2>&1
+            if grep -qe '^ *LIB *=' "$MakeDir/files" 2>/dev/null
             then
                 # If running in parallel start wmakeLnInclude on nCores
                 # and more as the cores become free
@@ -159,12 +156,6 @@ then
     sleep 2
 fi
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wmakeScheduler b/wmake/wmakeScheduler
index a927af7a3780d8e02bfa3e436f638202e3c9467d..98e645baf3ed9a65d4fc8b6322bc27659e8cdf13 100755
--- a/wmake/wmakeScheduler
+++ b/wmake/wmakeScheduler
@@ -237,12 +237,6 @@ then
     setterm -foreground default
 fi
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wmakeSchedulerUptime b/wmake/wmakeSchedulerUptime
index 6239f9e65c42c9978da63803354a3708057eddf1..19a346a8097f1324f2699b3f780ac3f2a3aa3814 100755
--- a/wmake/wmakeSchedulerUptime
+++ b/wmake/wmakeSchedulerUptime
@@ -252,12 +252,6 @@ do
     sleep 1
 done
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wrmdep b/wmake/wrmdep
index 790d6ed906a9d8ca3ade8bf8b2803b22a84688b4..c2f648e74e5ab7f17de703f1b7393390bca27775 100755
--- a/wmake/wrmdep
+++ b/wmake/wrmdep
@@ -4,7 +4,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2015-2016 OpenFOAM Foundation
-#    \\/     M anipulation  |
+#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 #-------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM.
@@ -25,21 +25,21 @@
 # Script wrmdep
 #
 # Usage
-#     wrmdep [-a | -all | all] [file]
-#     wrmdep [-o | -old] [dir1 .. dirN]
+#     wrmdep [-a | -all | all] [file1 [..fileN]]
+#     wrmdep [-o | -old] [[dir1 [..dirN]]
 #     wrmdep -update
 #
 # Description
 #     This is a catch-all script for pruning .dep files, depending on the
 #     provided arguments.
 #
-#     [-a | -all | all] [file]:
+#     [-a | -all | all] [file1 [.. fileN]]:
 #     Remove all .dep files from the object directory tree corresponding to the
-#     current source derectory or remove only the .dep files referring to the
-#     optionally specified [file].  With the -a/-all/all option the .dep files
+#     current source directory or remove only the .dep files referring to the
+#     optionally specified file(s).  With the -a/-all/all option the .dep files
 #     are removed for all platforms rather than just the current platform.
 #
-#     [-o | -old] [dir1 .. dirN]:
+#     [-o | -old] [dir1 [.. dirN]]:
 #     Remove *.dep files that are without a corresponding .C or .L source file.
 #     This occurs when a directory has been moved.
 #         - prints the questionable directory and *.dep file
@@ -48,10 +48,11 @@
 #     Search all the "src" and "application" directories of the project for
 #     broken symbolic links for source code files and then remove all .dep
 #     files that relate to files that no longer exist.
-#     Must be executed in the project top-level directory: $WM_PROJECT_DIR.
+#     Must be executed in the project top-level directory:
+#         $WM_PROJECT_DIR.
 #
 #------------------------------------------------------------------------------
-Script=${0##*/}
+Script=${0##*/}     # Note: need 'Script' for some functions in wmakeFunctions
 
 # Source the wmake functions
 . ${0%/*}/scripts/wmakeFunctions
@@ -62,26 +63,27 @@ usage() {
     cat<<USAGE
 Usage:
 
-    $Script [-a | -all | all] [file]
+$Script [-a | -all | all] [file1 [..fileN]]
 
-        Remove all .dep files or remove .dep files referring to <file>
-        With the -a/-all/all option the .dep files are removed for all
-        platform rather than just the current platform.
+    Remove all .dep files or remove .dep files referring to <file>
+    With the -a/-all/all option the .dep files are removed for all
+    platforms rather than just the current platform ($WM_OPTIONS).
 
-    $Script [-o | -old] [dir1 .. dirN]
+$Script [-o | -old] [dir1 [..dirN]]
 
-        Remove *.dep files that are without a corresponding .C or .L file.
-        This occurs when a directory has been moved.
-          - prints the questionable directory and *.dep file
+    Remove *.dep files that are without a corresponding .C or .L file.
+    This occurs when a directory has been moved.
+      - prints the questionable directory and *.dep file
 
-        Note: to remove empty directories, run: wclean empty
+    Note: to remove empty directories, run: wclean empty
 
-    $Script -update
+$Script -update
 
-        Search all the "src" and "application" directories of the project for
-        broken symbolic links for source code files and then remove all .dep
-        files that relate to files that no longer exist.
-        Must be executed in the project top-level directory: $WM_PROJECT_DIR
+    Search all the "src" and "application" directories of the project for
+    broken symbolic links for source code files and then remove all .dep
+    files that relate to files that no longer exist.
+    Must be executed in the project top-level directory:
+        $WM_PROJECT_DIR
 
 USAGE
     exit 1
@@ -93,10 +95,10 @@ USAGE
 #------------------------------------------------------------------------------
 
 # Default is for removing all .dep files in the current directory
-rmdepMode="files"
+rmdepMode=files
 
 # Default to processing only the current platform
-all=
+unset all
 
 while [ "$#" -gt 0 ]
 do
@@ -105,27 +107,28 @@ do
     -h | -help)
         usage
         ;;
-    # Non-stop compilation, ignoring errors
+    # All platforms
     -a | -all | all)
-        all="all"
+        all=all
         shift
         ;;
     -o | -old)
-        rmdepMode="oldFolders"
+        rmdepMode=oldFolders
         shift
         ;;
     -update)
-        rmdepMode="updateMode"
+        rmdepMode=updateMode
         shift
         ;;
     -*)
-       usage "unknown option: '$*'"
-       ;;
+        usage "unknown option: '$1'"
+        ;;
     *)
-       break
-       ;;
+        break
+        ;;
     esac
 done
+#------------------------------------------------------------------------------
 
 # Check environment variables
 checkEnv
@@ -141,7 +144,7 @@ files)
     findObjectDir .
 
     # With the -a/-all option replace the current platform with a wildcard
-    if [ "$all" = "all" ]
+    if [ "$all" = all ]
     then
         objectsDir=$(echo $objectsDir | sed s%$WM_OPTIONS%*% )
     fi
@@ -151,19 +154,21 @@ files)
         echo "removing all .dep files ..."
         find $objectsDir -name '*.dep' -print | xargs -t rm 2>/dev/null
     else
-        echo "removing .dep files referring to $1 ..."
-        find $objectsDir -name '*.dep' -exec grep -q "$1" '{}' \; \
-             -exec rm '{}' \; -print
+        echo "removing .o files corresponding to"
+        echo "    $@ ..."
+        for file
+        do
+            find $objectsDir -name '*.dep' -exec grep -q "$file" '{}' \; \
+                 -exec rm '{}' \; -print
+        done
     fi
-
     ;;
 
 oldFolders)
-
     # Default is the current directory
     [ "$#" -gt 0 ] || set -- .
 
-    echo "Removing dep files that refer to sources files that no longer exist..."
+    echo "Removing dep files that refer to source files that no longer exist..."
 
     for checkDir
     do
@@ -182,21 +187,19 @@ oldFolders)
             depToSource $depFile
 
             # Check C++ or Flex source file exists
-            if [ ! -r "$sourceFile" ];
+            if [ ! -r "$sourceFile" ]
             then
                 echo "    rm $depFile"
                 rm -f $depFile 2>/dev/null
             fi
         done
     done
-
     ;;
 
 updateMode)
-
     if [ "$PWD" != "$WM_PROJECT_DIR" ]
     then
-        echo "Cannot 'update', not in the project top-level directory"
+        echo "Cannot 'update', not in the project top-level directory" 1>&2
         exit 1
     fi
 
@@ -206,30 +209,19 @@ updateMode)
     for filePathAndName in $fileNameList
     do
         fileName=$(basename $filePathAndName)
-        echo "    'src': $fileName"
-        cd src
-        $Script -a $fileName
-
-        echo "    'applications': $fileName"
-        cd ../applications
-        $Script -a $fileName
-
-        cd ..
+        for subdir in src applications
+        do
+            echo "    '$subdir': $fileName"
+            (cd $subdir && $Script -a $fileName)
+        done
 
         # Just in case, remove the symbolic link as the last step
         unlink $filePathAndName
     done
-
     ;;
 
 esac
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage rmdepMode all
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------
diff --git a/wmake/wrmo b/wmake/wrmo
index b7cab9bf9846a733e738a246e6ee2564a635e397..01610c0f108868e819e574a916f97d75551b4bb0 100755
--- a/wmake/wrmo
+++ b/wmake/wrmo
@@ -4,7 +4,7 @@
 # \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
 #  \\    /   O peration     |
 #   \\  /    A nd           | Copyright (C) 2015 OpenFOAM Foundation
-#    \\/     M anipulation  |
+#    \\/     M anipulation  | Copyright (C) 2017 OpenCFD Ltd.
 #-------------------------------------------------------------------------------
 # License
 #     This file is part of OpenFOAM.
@@ -26,7 +26,7 @@
 #     wrmo
 #
 # Usage
-#     wrmo [-a | -all | all] [file]
+#     wrmo [-a | -all | all]  [file1 [... fileN]]
 #
 # Description
 #     Remove all .o files from the object directory tree corresponding to the
@@ -35,7 +35,7 @@
 #     are removed for all platforms rather than just the current platform.
 #
 #------------------------------------------------------------------------------
-Script=${0##*/}
+Script=${0##*/}     # Note: need 'Script' for some functions in wmakeFunctions
 
 # Source the wmake functions
 . ${0%/*}/scripts/wmakeFunctions
@@ -44,7 +44,11 @@ usage() {
     exec 1>&2
     while [ "$#" -ge 1 ]; do echo "$1"; shift; done
     cat<<USAGE
-Usage: $Script [file]
+Usage: $Script [OPTION] [file1 [... fileN]]
+
+options:
+  -a | -all         All platforms (current: $WM_OPTIONS)
+  -h | -help        Print the usage
 
 Remove all .o files or remove .o file corresponding to <file>
 
@@ -58,7 +62,7 @@ USAGE
 #------------------------------------------------------------------------------
 
 # Default to processing only the current platform
-all=
+unset all
 
 while [ "$#" -gt 0 ]
 do
@@ -66,17 +70,17 @@ do
     -h | -help)
         usage
         ;;
-    # Non-stop compilation, ignoring errors
+    # All platforms
     -a | -all | all)
-        all="all"
+        all=all
         shift
         ;;
     -*)
-       usage "unknown option: '$*'"
-       ;;
+        usage "unknown option: '$1'"
+        ;;
     *)
-       break
-       ;;
+        break
+        ;;
     esac
 done
 
@@ -91,7 +95,7 @@ checkEnv
 findObjectDir .
 
 # With the -a/-all option replace the current platform with a wildcard
-if [ "$all" = "all" ]
+if [ "$all" = all ]
 then
     objectsDir=$(echo $objectsDir | sed s%$WM_OPTIONS%*% )
 fi
@@ -101,16 +105,14 @@ then
     echo "removing all .o files ..."
     find $objectsDir -name '*.o' -print | xargs -t rm 2>/dev/null
 else
-    echo "removing .o files corresponding to $1 ..."
-    rm $objectsDir/${1%%.*}.o
+    echo "removing .o files corresponding to"
+    echo "    $@ ..."
+    for file
+    do
+        rm $objectsDir/${file%%.*}.o
+    done
 fi
 
-
-#------------------------------------------------------------------------------
-# Cleanup local variables and functions
-#------------------------------------------------------------------------------
-
-unset Script usage
-
+exit 0 # clean exit
 
 #------------------------------------------------------------------------------