diff --git a/.gitlab-ci-simple.yml b/.gitlab-ci-simple.yml
index 0c06eb0a10c7dddf8527ba8d05c4a653f6d11646..fe9bffce03ac46789526ae8ffd39ad7fd6bc13ea 100644
--- a/.gitlab-ci-simple.yml
+++ b/.gitlab-ci-simple.yml
@@ -1,6 +1,6 @@
 
 # see https://hub.docker.com/repository/docker/konttipoju/tinytex
-image: konttipoju/tinytex:2019-12g
+image: konttipoju/tinytex:2020-04a
 
 # first we build the PDF, then validate it
 stages:
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d815eb7f08462fd213e8954630b50146a5c71243..72723b89b4d49f2065589f820c203ee40f94aeab 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -8,7 +8,7 @@
 # files. In addition, minted requires some tuning to support different
 # output directories.
 
-image: konttipoju/tinytex:2019-12g
+image: konttipoju/tinytex:2020-04a
 
 stages:
  - prepare
diff --git a/Dockerfile b/Dockerfile
index 78ff35a8d0508da3471f72dcdd8ecc36514f70c4..275e733a9a8d2add766546e7faf0979782a8d8d5 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -6,7 +6,7 @@ ENV DEBIAN_FRONTEND noninteractive
 
 # combine into one run command to reduce image size
 RUN mkdir -p /usr/share/man/man1 && apt-get update && \
-    apt-get install -y perl wget libfontconfig1 openjdk-11-jre-headless poppler-utils python3-pygments && \
+    apt-get install -y perl wget libfontconfig1 openjdk-11-jre-headless poppler-utils python3-pygments pandoc && \
     wget -qO- "https://yihui.name/gh/tinytex/tools/install-unx.sh" | sh  && \
     apt-get clean
     
@@ -16,11 +16,11 @@ RUN tlmgr update --self && tlmgr install xetex scheme-small && fmtutil-sys --all
 
 # install only the packages you need
 RUN tlmgr install adjustbox collectbox biblatex tracklang biblatex-ieee csquotes datetime2 \
-    datetime2-english datetime2-finnish biber xstring units lastpage comment genmisc \
+    datetime2-english datetime2-finnish biber xstring units lastpage comment \
     fontawesome chessboard skak glossaries datenumber datetime advdate multido todonotes \
     semantic syntax wasysym dashrule titlecaps pdfx ifnextok xmpincl blindtext minted \
     fvextra catchfile includernw algorithm2e shapepar hanging sectionbox euro nag nomencl \
-    filecontents && tlmgr path add
+    filecontents zref && tlmgr path add
 
 RUN wget http://ftdev.utu.fi/downloads/resources/greenfield-apps-1.15.0-SNAPSHOT.jar -O /validator.jar && \
     echo 'java -cp /validator.jar org.verapdf.apps.GreenfieldCliWrapper --format text -v "$@"' > /root/bin/pdfa-validate && \
diff --git a/latex/utuftthesis.cls b/latex/utuftthesis.cls
index 3c2e6909fdda0ad22190bda60c3626bd9298e373..bafe71dcefb714138566aca1309624de74f61974 100644
--- a/latex/utuftthesis.cls
+++ b/latex/utuftthesis.cls
@@ -33,14 +33,14 @@
 \ProvidesClass{utuftthesis}[2019/05/28 UTU FT dept BSc/MSc thesis]
 
 %% package requirements for this template
-\RequirePackage{kvoptions}        % keyval-options, required by this class definition
-\RequirePackage{adjustbox}        % front page box on the right
-\RequirePackage{etoolbox}         % misc. tools
-\RequirePackage{ifthen}           % mainly: \langchoose and \switch (\ifstrequal doesn't work here?)
-\RequirePackage{ifxetex}          % for xelatex/pdflatex font selection
-\RequirePackage{fancyhdr}         % customized headers/footers
-\RequirePackage{xstring}          % string operations (parse the number of pages etc.)
-\RequirePackage[page]{totalcount} % the total number of pages (another option: zref-lastpage, @see https://tex.stackexchange.com/a/220564)
+\RequirePackage{kvoptions}                    % keyval-options, required by this class definition
+\RequirePackage{adjustbox}                    % front page box on the right
+\RequirePackage{etoolbox}                     % misc. tools
+\RequirePackage{ifthen}                       % mainly: \langchoose and \switch (\ifstrequal doesn't work here?)
+\RequirePackage{ifxetex}                      % for xelatex/pdflatex font selection
+\RequirePackage{fancyhdr}                     % customized headers/footers
+\RequirePackage{xstring}                      % string comparison
+\RequirePackage[lastpage,totpages,user]{zref} % page counting logic
 
 %% Custom package options
 %\DeclareStringOption[00]{year}
@@ -132,8 +132,6 @@
       \setmainfont{\utuftthesis@mainfont}
     }
   }
-\fi
-\ifxetex
 \else
   % JH: modified latin to UTF-8 encoding cues to make Scandinavian characters work
   \RequirePackage[T1]{fontenc}
@@ -369,11 +367,7 @@
   
 %% Modify the \printbibliography command
 \defbibheading{bibliography}[\refs]{
-  \ifutuftthesis@countbibpages
-  \else
-    % label here if we don't include the bibliography in the page count
-    \label{endofpages}
-  \fi
+  \zlabel{endofmaincontent}
   \chapter*{#1}
   \markboth{\MakeUppercase{#1}}{}
   \addcontentsline{toc}{chapter}{#1}
@@ -384,10 +378,7 @@
 
 \renewcommand{\printbibliography}{
   \oldprintbibliography
-    % label here if we include the bibliography in the page count
-  \ifutuftthesis@countbibpages
-    \label{endofpages}
-  \fi
+  \zlabel{endofbibliography}
 \langfin{
   \expandafter\selectlanguage\expandafter{\n@tivelang}
 }{
@@ -470,10 +461,6 @@
   \clearpage
 }
 
-%% used to detect the empty \totalpages on the first latex run
-%% @see http://ftp.acc.umu.se/mirror/CTAN/macros/generic/xstring/xstring-en.pdf
-\newcommand*{\IsInteger}[3]{\IfStrEq{#1}{ }{}{\IfInteger{#1}{#2}{#3}}}
-
 %% top part of the abstract page (since we use environments)
 \newcommand{\@bstracth}[1]{%
   \selectlanguage{#1}
@@ -491,15 +478,14 @@
   \item[\textsc{\@author}] \@title
   \end{entry}
   \thesistype{#1}, %
-  \IsInteger{\totalpages}{%
-    \pageref{endofpages} \pagesname{#1}%
-    \ifnum \totalpages > 0
-      , \totalpages\ \apppagesname{#1} \\
-    \else
-      \\
-    \fi
-  }{ \runagain{#1} \\
-  }
+  \ifutuftthesis@countbibpages
+    \zref@extractdefault{endofbibliography}{page}{\zref@extractdefault{LastPage}{page}{0}} \pagesname{#1}%
+  \else
+    \zref@extractdefault{endofmaincontent}{page}{\zref@extractdefault{LastPage}{page}{0}} \pagesname{#1}%
+  \fi
+  \newcommand\lastapppage{\zref@extractdefault{appendices}{abspage}{999}}%
+  \newcommand\apppagecount{\number\numexpr \ztotpages - \lastapppage + 1\relax}%
+  \IfStrEq*{\lastapppage}{999}{}{, \apppagecount\ \apppagesname{#1}} \\
   \labname{#1} \\
   \monthname{#1}{\m@nth} \ye@r\\
   \rule{\linewidth}{1 pt}
@@ -557,9 +543,6 @@
   \rhead{\thepage}
   \fancyfoot{}
   \cfoot{\version}
-  % the totalcount counter is only used for counting the appendices
-  % warning: fails if the document has over 65000 pages
-  \setcounter{totalcount@page}{-65000}
 }       
 
 %% postpone the doc style through these sections
@@ -582,18 +565,16 @@
   \renewcommand{\appendices}{}
   \clearpage
   \appendix
+  \zlabel{appendices}
   \addtocontents{toc}{\protect\contentsline{chapter}{\appname}{}\relax}
   \renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{\appendixname\ \thechapter.\ ##1}}{}}
   \rhead{\thechapter-\arabic{page}}
-  \setcounter{page}{0}
   \renewcommand\thepage{\thechapter-\arabic{page}}
-  \setcounter{totalcount@page}{\utuftthesis@pagecountdelta}
 }
 
 %% use in place of \chapter
 \newcommand{\appchapter}[1]{%
   \appendices
-  \addtocounter{totalcount@page}{\value{page}}
   \clearpage
   \setcounter{page}{1}
   \chapter{#1}