diff --git a/mythtv/programs/mythfrontend/themechooser.cpp b/mythtv/programs/mythfrontend/themechooser.cpp
index 0dd292c..56d6d66 100644
--- a/mythtv/programs/mythfrontend/themechooser.cpp
+++ b/mythtv/programs/mythfrontend/themechooser.cpp
@@ -151,13 +151,6 @@ void ThemeChooser::Load(void)
 {
     SetBusyPopupMessage(tr("Loading Installed Themes"));
 
-    QString MythVersion = MYTH_SOURCE_PATH;
-
-    // Treat devel branches as master
-    if (!MythVersion.isEmpty() && !MythVersion.startsWith("fixes/"))
-        // FIXME: For now, treat git master the same as svn trunk
-        MythVersion = "trunk";
-
     QStringList themesSeen;
     QDir themes(m_userThemeDir);
     themes.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
@@ -191,6 +184,19 @@ void ThemeChooser::Load(void)
         }
     }
 
+    // MYTH_SOURCE_VERSION - examples v29-pre-574-g92517f5, v29-Pre, v29.1-21-ge26a33c
+    QString MythVersion(MYTH_SOURCE_VERSION);
+    QRegExp trunkver("v[0-9]+-pre.*",Qt::CaseInsensitive);
+    QRegExp validver("v[0-9]+.*",Qt::CaseInsensitive);
+
+    if (!validver.exactMatch(MythVersion))
+    {
+        LOG(VB_GENERAL, LOG_ERR, QString("Invalid MythTV version %1, will use themes from trunk").arg(MythVersion));
+        MythVersion = "trunk";
+    }
+    if (trunkver.exactMatch(MythVersion))
+        MythVersion = "trunk";
+
     if (MythVersion == "trunk")
     {
         LoadVersion(MythVersion, themesSeen, true);
@@ -200,12 +206,15 @@ void ThemeChooser::Load(void)
     {
 
         MythVersion = MYTH_BINARY_VERSION; // Example: 29.20161017-1
+        // Remove the date part and the rest, eg 29.20161017-1 -> 29
         MythVersion.replace(QRegExp("\\.[0-9]{8,}.*"), "");
         LOG(VB_GUI, LOG_INFO, QString("Loading themes for %1").arg(MythVersion));
         LoadVersion(MythVersion, themesSeen, true);
 
         // If a version of the theme for this tag exists, use it...
+        // MYTH_SOURCE_VERSION - examples v29-pre-574-g92517f5, v29-Pre, v29.1-21-ge26a33c
         QRegExp subexp("v[0-9]+\\.([0-9]+)-*");
+        // This captures the subversion, i.e. the number after a dot
         int pos = subexp.indexIn(MYTH_SOURCE_VERSION);
         if (pos > -1)
         {
diff --git a/mythtv/version.sh b/mythtv/version.sh
index 37e42f1..3ffd4ee 100755
--- a/mythtv/version.sh
+++ b/mythtv/version.sh
@@ -3,7 +3,6 @@
 # small shell script to generate version.h
 # it expects one parameter
 # first parameter is the root of the source directory
-
 if test $# -ne 1; then
     echo "Usage: version.sh GIT_TREE_DIR"
     exit 1
@@ -29,7 +28,27 @@ case "${SOURCE_VERSION}" in
     exported|Unknown)
         if ! grep -q Format $GITTREEDIR/EXPORTED_VERSION; then
             . $GITTREEDIR/EXPORTED_VERSION
+            # This file has SOURCE_VERSION and BRANCH
+            # example SOURCE_VERSION="30d8a96"
+            # BRANCH examples from github
+            # BRANCH=" (HEAD -> master)"
+            # BRANCH=" (fixes/0.28)"
+            # BRANCH=" (tag: v0.28.1)"
+            # From a checkout they can be as follows:
+            # " (origin/fixes/0.28, fixes/0.28)"
+            # " (HEAD -> master, origin/master, origin/HEAD)"
+            # " (tag: v0.28.1)"
+            hash="$SOURCE_VERSION"
+            # This extracts after the last comma inside the parens:
             BRANCH=$(echo "${BRANCH}" | sed 's/ (\(.*, \)\{0,1\}\(.*\))/\2/')
+            # Create a suitable version (hash is no good)
+            SOURCE_VERSION="$BRANCH"
+            SOURCE_VERSION=`echo "$SOURCE_VERSION" | sed "s/tag: *//"`
+            if ! echo "$SOURCE_VERSION" | grep "^v[0-9]" ; then
+                . $GITTREEDIR/VERSION
+            fi
+            SOURCE_VERSION="${SOURCE_VERSION}-${hash}"
+            echo "Source Version created as $SOURCE_VERSION"
         elif test -e $GITTREEDIR/VERSION ; then
             . $GITTREEDIR/VERSION
         fi
@@ -41,6 +60,23 @@ case "${SOURCE_VERSION}" in
     ;;
 esac
 
+if ! echo "${SOURCE_VERSION}" | egrep -i "v[0-9]+.*"   ; then
+    # Invalid version - use VERSION file
+    echo "WARNING: Invalid source version ${SOURCE_VERSION}, must start with v and a number, will use VERSION file instead"
+    . $GITTREEDIR/VERSION
+fi
+
+src_vn=`echo "${SOURCE_VERSION}" | sed "s/^[Vv]// ; s/-.*// ; s/\..*//"`
+MYTH_BINARY_VERSION=`grep "#define *MYTH_BINARY_VERSION" libs/libmythbase/mythversion.h \
+  | sed 's/#define// ; s/MYTH_BINARY_VERSION// ; s/ //g ; s/"//g'`
+
+bin_vn=`echo $MYTH_BINARY_VERSION | sed 's/\..*//'`
+
+if ! test $src_vn -eq $bin_vn ; then
+    echo "ERROR: High level of source version ${SOURCE_VERSION}, does not match high level of binary version $MYTH_BINARY_VERSION"
+    exit 2
+fi
+
 cat > .vers.new <<EOF
 #ifndef MYTH_SOURCE_VERSION
 #define MYTH_SOURCE_VERSION "${SOURCE_VERSION}"
