diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5e0057ced14719018cc4865993ac8670182640b5..22bb9d6ade82df9c31c2578ad85b929df106e745 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -8,32 +8,87 @@ stages:
  - build
  - test
 
-pdflatex_builder:
+template_builder:
   stage: build
   before_script:
     - cd latex
   script:
     - "latexmk -pdf -output-directory=pdflatex/ thesis.tex"
+    - "latexmk -xelatex -output-directory=xelatex/ thesis.tex"
   artifacts:
     paths:
       - "latex/pdflatex/thesis.pdf"
+      - "latex/xelatex/thesis.pdf"
+
+template_validator:
+  stage: test
+  script:
+    - "pdfinfo latex/pdflatex/thesis.pdf |grep '^Pages: *13$'"
+    - "pdfinfo latex/xelatex/thesis.pdf |grep '^Pages: *13$'"
 
 xelatex_builder:
   stage: build
   before_script:
-    - cd latex
+    - cd tests
   script:
-    - "latexmk -xelatex -output-directory=xelatex/ thesis.tex"
+    - "for i in *.tex; do latexmk -xelatex -output-directory=xelatex/ $i; done"
   artifacts:
     paths:
-      - "latex/xelatex/thesis.pdf"
+      - tests/xelatex/test-app1.pdf
+      - tests/xelatex/test-app2.pdf
+      - tests/xelatex/test-app3.pdf
+      - tests/xelatex/test-app4.pdf
+      - tests/xelatex/test-bilingual.pdf
+      - tests/xelatex/test-english.pdf
+      - tests/xelatex/test-finnish.pdf
+      - tests/xelatex/test-hyperref.pdf
 
-pdflatex_content_validator:
+pdflatex_builder:
+  stage: build
+  before_script:
+    - cd tests
+  script:
+    - "for i in *.tex; do latexmk -pdf -output-directory=pdflatex/ $i; done"
+  artifacts:
+    paths:
+      - tests/pdflatex/test-app1.pdf
+      - tests/pdflatex/test-app2.pdf
+      - tests/pdflatex/test-app3.pdf
+      - tests/pdflatex/test-app4.pdf
+      - tests/pdflatex/test-bilingual.pdf
+      - tests/pdflatex/test-english.pdf
+      - tests/pdflatex/test-finnish.pdf
+      - tests/pdflatex/test-hyperref.pdf
+      
+xelatex_content_validator:
   stage: test
+  before_script:
+    - cd tests/xelatex
+    - "for i in *.pdf; do pdftotext $i; done"
   script:
-    - "pdfinfo latex/pdflatex/thesis.pdf |grep '^Pages: *13$'"
+    - "grep '2 s., 5 liites.' test-app1.txt"
+    - "grep '2 s., 3 liites.' test-app2.txt"
+    - "grep '2 s., 1 liites.' test-app3.txt"
+    - "grep 'TkK -tutkielma, 2 s.$' test-app4.txt"
+    - "grep 'TURUN YLIOPISTO' test-finnish.txt"
+    - "grep 'TURUN YLIOPISTO' test-bilingual.txt"
+    - "grep 'UNIVERSITY OF TURKU' test-bilingual.txt"
+    - "grep 'UNIVERSITY OF TURKU' test-english.txt"
+    - "pdfinfo test-hyperref.pdf |grep 'LaTeX with hyperref package'"
 
-xelatex_content_validator:
+
+pdflatex_content_validator:
   stage: test
+  before_script:
+    - cd tests/pdflatex
+    - "for i in *.pdf; do pdftotext $i; done"
   script:
-    - "pdfinfo latex/xelatex/thesis.pdf |grep '^Pages: *13$'"
+    - "grep '2 s., 5 liites.' test-app1.txt"
+    - "grep '2 s., 3 liites.' test-app2.txt"
+    - "grep '2 s., 1 liites.' test-app3.txt"
+    - "grep 'TkK -tutkielma, 2 s.$' test-app4.txt"
+    - "grep 'TURUN YLIOPISTO' test-finnish.txt"
+    - "grep 'TURUN YLIOPISTO' test-bilingual.txt"
+    - "grep 'UNIVERSITY OF TURKU' test-bilingual.txt"
+    - "grep 'UNIVERSITY OF TURKU' test-english.txt"
+    - "pdfinfo test-hyperref.pdf |grep 'LaTeX with hyperref package'"
diff --git a/latex/thesis.lyx b/latex/thesis.lyx
index 4c0dbcbe61fe64a42f007d5872837e1341a7812c..548ce2ddc033783ebd26f360ef2b052a64366c94 100644
--- a/latex/thesis.lyx
+++ b/latex/thesis.lyx
@@ -5,7 +5,7 @@
 \save_transient_properties true
 \origin unavailable
 \textclass utuftthesis
-\options a4paper,12pt,language=finnish,hidechapters=true,realtimesnewroman=false
+\options a4paper,12pt,language=finnish,version=draft,hidechapters=true,includereferences=false,realtimesnewroman=false,sharelatex=false,emptyfirstpages=true
 \use_default_options false
 \maintain_unincluded_children false
 \language finnish
@@ -146,22 +146,22 @@ The utuftthesis.cls defines a new thesis class, which is based on the report
 \end_layout
 
 \begin_layout Plain Layout
-- language: finnish / english (default:finnish) affects the general document
+- language: finnish / english (default: finnish) affects the general document
  appearance and hyphenation
 \end_layout
 
 \begin_layout Plain Layout
-- hidechapters: true / false (default:true) hides/shows the chapter/luku
+- hidechapters: true / false (default: true) hides/shows the chapter/luku
  text at the beginning of each Chapter
 \end_layout
 
 \begin_layout Plain Layout
-- includereferences: true / false (default:false) include reference pages
+- includereferences: true / false (default: false) include reference pages
  when calculating the total number of pages
 \end_layout
 
 \begin_layout Plain Layout
-- realtimesnewroman: true / false (default:false) use Times New Roman instead
+- realtimesnewroman: true / false (default: false) use Times New Roman instead
  of LaTeX fonts with XeLaTeX.
  Requires the font to be installed on the system / provided in the document
  directory.
@@ -173,13 +173,18 @@ setmainfont.
 \begin_layout Plain Layout
 - sharelatex: true / false (default: false) don't attempt to use (c) system
  fonts, instead read them from the project repository
-\begin_inset Newline newline
-\end_inset
+\end_layout
 
+\begin_layout Plain Layout
+- emptyfirstpages: true / false (default: true) clear the headers/footers
+ for the 1st pages of text chapters
+\end_layout
 
-\begin_inset Newline newline
-\end_inset
+\begin_layout Plain Layout
 
+\end_layout
+
+\begin_layout Plain Layout
 Traditionally the best places to learn (La)TeX are probably the manual pages
  for each package http://www.ctan.org/ and http://www.ctan.org/tex-archive/info/lsho
 rt/english/lshort.pdf.
@@ -248,28 +253,10 @@ Relies on utuftthesis.cls for the document class definitions.
 
 \begin_layout PubLab
 Labran nimi
-\begin_inset ERT
-status open
-
-\begin_layout Plain Layout
-
-}{Laboratory Name
-\end_layout
-
-\end_inset
-
-
-\begin_inset Note Note
-status open
-
-\begin_layout Plain Layout
-looks a bit funny in LyX.
- Gotta learn how to fix this..
 \end_layout
 
-\end_inset
-
-
+\begin_layout PubLabEN
+Laboratory Name
 \end_layout
 
 \begin_layout PubType
@@ -295,47 +282,17 @@ filename "abstract.lyx"
 \end_layout
 
 \begin_layout Standard
-\begin_inset Note Comment
-status open
-
-\begin_layout Plain Layout
-
-\end_layout
-
-\end_inset
-
-
 \begin_inset ERT
 status open
 
 \begin_layout Plain Layout
 
 % empty pagestyle for table of contents etc.
- 
 \end_layout
 
 \begin_layout Plain Layout
 
-% the redefinition of plain style works also for 1st pages of chapters,
-\end_layout
-
-\begin_layout Plain Layout
-
-% which is the default in report class.
-\end_layout
-
-\begin_layout Plain Layout
-
-% Just state 
-\backslash
-thispagestyle{empty} after 
-\backslash
-chapter{something} if you want
-\end_layout
-
-\begin_layout Plain Layout
-
-% empty style for the 1st pages.
+% otherwise you'll get simple page style with roman page numbers
 \end_layout
 
 \begin_layout Plain Layout
@@ -347,90 +304,11 @@ pagestyle{empty}
 
 \begin_layout Plain Layout
 
-
-\backslash
-fancypagestyle{plain}{
-\end_layout
-
-\begin_layout Plain Layout
-
-  
-\backslash
-fancyhf{}
-\end_layout
-
-\begin_layout Plain Layout
-
-  
-\backslash
-renewcommand{
-\backslash
-headrulewidth}{0 pt}
-\end_layout
-
-\begin_layout Plain Layout
-
-}
-\end_layout
-
-\end_inset
-
-
-\begin_inset ERT
-status open
-
-\begin_layout Plain Layout
-
-% roman numbering for table of contents etc.
-\end_layout
-
-\begin_layout Plain Layout
-
-
-\backslash
-pagenumbering{roman}
-\end_layout
-
-\begin_layout Plain Layout
-
-\end_layout
-
-\begin_layout Plain Layout
-
-% insert table of contents, list of figures, list of tables
-\end_layout
-
-\begin_layout Plain Layout
-
-%
-\end_layout
-
-\begin_layout Plain Layout
-
-% setting the counter to zero effectively removes the page number from
-\end_layout
-
-\begin_layout Plain Layout
-
-% the toc, lof etc.
- entries in the toc since there is no roman numeral
-\end_layout
-
-\begin_layout Plain Layout
-
-% for zero ;-) (if you want them without numbering)
 \end_layout
 
 \begin_layout Plain Layout
 
-%
-\end_layout
-
-\begin_layout Plain Layout
-
-%
-\backslash
-setcounter{page}{0}
+% mandatory
 \end_layout
 
 \begin_layout Plain Layout
@@ -442,64 +320,48 @@ tableofcontents
 
 \begin_layout Plain Layout
 
-
-\backslash
-clearpage
 \end_layout
 
 \begin_layout Plain Layout
 
+% if you want a list of figures
 \end_layout
 
 \begin_layout Plain Layout
 
 %
 \backslash
-setcounter{page}{0}
+listoffigures
 \end_layout
 
 \begin_layout Plain Layout
 
-%
-\backslash
-listoffigures 
 \end_layout
 
 \begin_layout Plain Layout
 
-%
-\backslash
-clearpage
+% if you want a list of tables
 \end_layout
 
 \begin_layout Plain Layout
 
 %
 \backslash
-setcounter{page}{0}
+listoftables
 \end_layout
 
 \begin_layout Plain Layout
 
-%
-\backslash
-listoftables
 \end_layout
 
-\end_inset
-
-
-\begin_inset ERT
-status open
-
 \begin_layout Plain Layout
 
-% possibly insert 'list of acronyms'
+% 'list of acronyms'
 \end_layout
 
 \begin_layout Plain Layout
 
-%
+%   - you may not need this at all
 \end_layout
 
 \begin_layout Plain Layout
@@ -555,25 +417,7 @@ input{your acronym chapter's file name}
 
 %
 \backslash
-setcounter{secnumdepth}{2}
-\end_layout
-
-\end_inset
-
-
-\begin_inset ERT
-status open
-
-\begin_layout Plain Layout
-
-% setup page numbering, page counter, etc.
-\end_layout
-
-\begin_layout Plain Layout
-
-
-\backslash
-startpages
+setcounter{secnumdepth}{2}% setup page numbering, page counter, etc.
 \end_layout
 
 \end_inset
@@ -665,33 +509,6 @@ options "plain"
 \end_inset
 
 
-\begin_inset Note Comment
-status open
-
-\begin_layout Plain Layout
-Don't include this if appendices are not present.
- On the other hand, without this call, the appendices don't look right.
-\end_layout
-
-\end_inset
-
-
-\end_layout
-
-\begin_layout Standard
-\begin_inset ERT
-status open
-
-\begin_layout Plain Layout
-
-
-\backslash
-appendices 
-\end_layout
-
-\end_inset
-
-
 \end_layout
 
 \begin_layout Standard
diff --git a/latex/thesis.tex b/latex/thesis.tex
index cd6d370293c6410e100f69ed9b11140d5cc90d76..d2b39ce828761f46677d37a85f7f2f63c5372076 100644
--- a/latex/thesis.tex
+++ b/latex/thesis.tex
@@ -1,17 +1,6 @@
-%% LyX 2.3.1 created this file.  For more info, see http://www.lyx.org/.
-%% Do not edit unless you really know what you are doing.
-\documentclass[a4paper,12pt,language=finnish,hidechapters=true,realtimesnewroman=false]{utuftthesis}
+\documentclass[a4paper,12pt,language=finnish,version=draft,hidechapters=true,includereferences=false,realtimesnewroman=false,sharelatex=false,emptyfirstpages=true]{utuftthesis}
 \setcounter{secnumdepth}{2}
 \setcounter{tocdepth}{2}
-\usepackage{verbatim}
-
-\makeatletter
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
-%% Because html converters don't know tabularnewline
-\providecommand{\tabularnewline}{\\}
-
-\makeatother
 
 \addbibresource{Bibliografia.bib}
 \begin{document}
@@ -35,23 +24,26 @@ the report class. It supports these new named parameters:
 - version: draft / final (default: draft) shows/hides {[}draft{]}
 in the header
 
-- language: finnish / english (default:finnish) affects the general
+- language: finnish / english (default: finnish) affects the general
 document appearance and hyphenation
 
-- hidechapters: true / false (default:true) hides/shows the chapter/luku
+- hidechapters: true / false (default: true) hides/shows the chapter/luku
 text at the beginning of each Chapter
 
-- includereferences: true / false (default:false) include reference
+- includereferences: true / false (default: false) include reference
 pages when calculating the total number of pages
 
-- realtimesnewroman: true / false (default:false) use Times New Roman
+- realtimesnewroman: true / false (default: false) use Times New Roman
 instead of LaTeX fonts with XeLaTeX. Requires the font to be installed
 on the system / provided in the document directory. Other fonts can
 be defined with \textbackslash setmainfont.
 
 - sharelatex: true / false (default: false) don't attempt to use (c)
-system fonts, instead read them from the project repository\\
-\\
+system fonts, instead read them from the project repository
+
+- emptyfirstpages: true / false (default: true) clear the headers/footers
+for the 1st pages of text chapters
+
 Traditionally the best places to learn (La)TeX are probably the manual
 pages for each package http://www.ctan.org/ and http://www.ctan.org/tex-archive/info/lshort/english/lshort.pdf.
 This new version (2.0) should be compatible with xelatex and biblatex
@@ -78,7 +70,9 @@ Relies on utuftthesis.cls for the document class definitions.
 
 \pubmonth{6}
 
-\publab{Labran nimi}{Laboratory Name}
+\publab{Labran nimi}
+
+\publaben{Laboratory Name}
 
 \pubtype{tkk}
 \title{Name of Thesis}
@@ -87,36 +81,21 @@ Relies on utuftthesis.cls for the document class definitions.
 \maketitle
 \input{abstract.tex}
 
-\begin{comment}
-\end{comment}
-% empty pagestyle for table of contents etc. 
-% the redefinition of plain style works also for 1st pages of chapters,
-% which is the default in report class.
-% Just state \thispagestyle{empty} after \chapter{something} if you want
-% empty style for the 1st pages.
+% empty pagestyle for table of contents etc.
+% otherwise you'll get simple page style with roman page numbers
 \pagestyle{empty}
-\fancypagestyle{plain}{
-  \fancyhf{}
-  \renewcommand{\headrulewidth}{0 pt}
-}% roman numbering for table of contents etc.
-\pagenumbering{roman}
 
-% insert table of contents, list of figures, list of tables
-%
-% setting the counter to zero effectively removes the page number from
-% the toc, lof etc. entries in the toc since there is no roman numeral
-% for zero ;-) (if you want them without numbering)
-%
-%\setcounter{page}{0}
+% mandatory
 \tableofcontents
-\clearpage
 
-%\setcounter{page}{0}
-%\listoffigures 
-%\clearpage
-%\setcounter{page}{0}
-%\listoftables% possibly insert 'list of acronyms'
-%
+% if you want a list of figures
+%\listoffigures
+
+% if you want a list of tables
+%\listoftables
+
+% 'list of acronyms'
+%   - you may not need this at all
 %   - create a chapter called List Of Acronyms (or whatever), which
 %     should contain all your acronym definitions, e.g. 
 %     \chapter{List Of Acronyms} 
@@ -125,8 +104,7 @@ Relies on utuftthesis.cls for the document class definitions.
 %
 %\setcounter{secnumdepth}{-1}
 %\input{your acronym chapter's file name}
-%\setcounter{secnumdepth}{2}% setup page numbering, page counter, etc.
-\startpages%
+%\setcounter{secnumdepth}{2}% setup page numbering, page counter, etc.%
 \begin{comment}
 The thesis starts here.
 
@@ -148,12 +126,6 @@ Good luck!
 The thesis main content ends here.
 \end{comment}
 \printbibliography
-\begin{comment}
-Don't include this if appendices are not present. On the other hand,
-without this call, the appendices don't look right.
-\end{comment}
-
-\appendices 
 
 \begin{comment}
 Create your appendix chapters with command \textbackslash appchapter\{some
diff --git a/latex/utuftthesis.cls b/latex/utuftthesis.cls
index 3413f578f9061beeb45cefc12b2c8fbce7760b95..13dbc9be3306dc503f7d63b7d00bd8075d8983a8 100644
--- a/latex/utuftthesis.cls
+++ b/latex/utuftthesis.cls
@@ -1,7 +1,13 @@
-% version 2.1 2018/10/02 Bugfixes: removed redundant tutkielma.tex, fixed issues with appendices, small caps, added an option to enable the truetype times new roman font
-% version 2.0 2018/08/29 new version using document classes, xelatex, biblatex, and some additional TeX scripting by Jari-Matti Mäkelä (jmjm@iki.fi), based on v1.3 by Sami Nuuttila (samnuutt@utu.fi), Johannes Holvitie (jjholv@utu.fi) et al.
-% version 1.3+ 1.10.2017//10/1 - 2016/9/22 additions marked "JH:", modified for use in tex.soft.utu.fi, Johannes Holvitie (jjholv@utu.fi)
-% version 1.3 2015/9/5 Sami Nuuttila (samnuutt@utu.fi)
+% version 2.2 2018/10/06 Bugfixes: Got rid of startappendices and startpages.
+%                                  various fixes. cleanups
+% version 2.1 2018/10/02 Bugfixes: removed redundant tutkielma.tex, fixed issues with
+%                                  appendices, small caps, added an option to enable
+%                                  the truetype times new roman font
+% version 2.0 2018/08/29 new version using document classes, xelatex, biblatex, and
+%                        additional TeX scripting. Jari-Matti Mäkelä (jmjm@iki.fi)
+% version 1.3+ 2017/10/01 - 2016/9/22 additions marked "JH:", modified for use in
+%                                     tex.soft.utu.fi, Johannes Holvitie (jjholv@utu.fi)
+% version 1.3 2015/09/05 Sami Nuuttila (samnuutt@utu.fi)
 
 % ------------------------------
 % How to use? Store the file in
@@ -14,21 +20,35 @@
 % ------------------------------
 
 \NeedsTeXFormat{LaTeX2e}[2007/01/01]
-\ProvidesClass{utuftthesis}[2018/10/02 UTU FT dept BSc/MSc thesis]
-
-\RequirePackage{kvoptions}
-
-\newcommand{\n@tivelang}{finnish}
+\ProvidesClass{utuftthesis}[2018/10/06 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}           % misc. tools
+\RequirePackage{ifxetex}          % for xelatex/pdflatex font selection
+\RequirePackage{fancyhdr}         % customized headers/footers
+\RequirePackage{xstring}          % string operations (number of pages etc.)
+\RequirePackage[page]{totalcount} % the total number of pages
+\RequirePackage{comment}          % LyX generated comments
+
+%% not used by the class
+% if you encounter package clashes, start by disabling these
+\RequirePackage{graphicx}        % \includegraphics{file}, works without file extensions
+\RequirePackage{sverb}           % enhanced handling of verbatim stuff
 
 %% Custom package options
 %\DeclareStringOption[00]{year}
 \DeclareStringOption[draft]{version}
 \DeclareStringOption[a4paper]{paper}
 \DeclareStringOption[finnish]{language}
-\DeclareStringOption[true]{hidechapters}
-\DeclareStringOption[false]{includereferences}
-\DeclareStringOption[false]{realtimesnewroman}
-\DeclareStringOption[false]{sharelatex}
+\DeclareBoolOption[true]{hidechapters}
+\DeclareBoolOption[false]{includereferences}
+\DeclareBoolOption[false]{realtimesnewroman}
+\DeclareBoolOption[false]{sharelatex}
+\DeclareBoolOption[true]{emptyfirstpages}
+
 
 %% Process given options
 \ProcessKeyvalOptions*
@@ -37,177 +57,129 @@
 %\DeclareOption*{}
 %  \ClassWarning{myclass}{Unknown option '\CurrentOption'}
 %\ProcessOptions\relax
+%\RequirePackage{ifpdf}           % not used
+%% automatically handle the appendix magic
+%\AtEndDocument{\addtocounter{totalcount@page}{-2}}
 
 %% Load base
 \LoadClass[\utuftthesis@paper,12pt]{report}
 
-\RequirePackage{ifthen}
-
 %% Some language macros for choosing between content
+%% the langchoose is now only good for \n@tivelang & english. easy to adjust
+%% for swedish & english, but multiple choices requires the use of switch-case
+\newcommand{\n@tivelang}{finnish}
+\newcommand{\l@nguage}{\utuftthesis@language}
+\newcommand{\thedoclanguage}{\utuftthesis@language}
+%\newcommand{\langchoose}[3]{\expandafter\ifdefstrequal\expandafter{#1}{\n@tivelang}{#2}{#3}}
 \newcommand{\langchoose}[3]{\ifthenelse{\equal{#1}{\n@tivelang}}{#2}{#3}}
-\newcommand{\langfin}[2]{\ifthenelse{\equal{\utuftthesis@language}{\n@tivelang}}{#1}{#2}}
-\newif\iffin
-\langfin{\fintrue}{\finfalse}
-
-\RequirePackage{ifxetex}
+\newcommand{\langfin}[2]{\langchoose{\l@nguage}{#1}{#2}}
 
 %% Set up encoding/font/hyphenation stuff
 \ifxetex
   \RequirePackage{fontspec}
   \RequirePackage{polyglossia}
   \setmainlanguage{\utuftthesis@language}
-  \iffin
-  \setotherlanguage{english}
-  \else
+  \langfin{\setotherlanguage{english}}
+  
+  \ifutuftthesis@realtimesnewroman
+    % can't use (C) TTF fonts when the ms corefonts are not installed
+    \ifutuftthesis@sharelatex
+      \setmainfont[BoldFont=timesbd.ttf,ItalicFont=timesi.ttf,BoldItalicFont=timesbi.ttf]{times.ttf}
+    \else
+      \setmainfont{Times New Roman}
+    \fi
   \fi
-  \ifthenelse{\equal{\utuftthesis@realtimesnewroman}{true}}{
-    \ifthenelse{\equal{\utuftthesis@sharelatex}{true}}{
-      \setmainfont[
-      BoldFont=timesbd.ttf,
-      ItalicFont=timesi.ttf,
-      BoldItalicFont=timesbi.ttf
-      ]{times.ttf}
-    }{
-    \setmainfont{Times New Roman}
-    }
-  }{}
+\fi
+\ifxetex
 \else
-% JH: modified latin to UTF-8 encoding cues to make Scandinavian characters works
+  % JH: modified latin to UTF-8 encoding cues to make Scandinavian characters works
   \RequirePackage[T1]{fontenc}
   \RequirePackage[utf8]{inputenc}
-% load babel-package for automatic hyphenation
-  \iffin
-  \RequirePackage[english, \utuftthesis@language]{babel}
-  \else
-  \RequirePackage[\utuftthesis@language]{babel}
-  \fi
   \RequirePackage{lmodern}
+
+  % load babel-package for automatic hyphenation
+  \ifdefstring{\utuftthesis@language}{english}{
+    \RequirePackage[english]{babel}
+  }{
+    \RequirePackage[english, \l@nguage]{babel}
+  }
+  
   % change default font to times-roman
   \renewcommand{\sfdefault}{phv}
   \renewcommand{\rmdefault}{ptm}
   \renewcommand{\ttdefault}{pcr}
 \fi
-
+  
+%% the rest of the requirements listed here as they have
+%% language specific definitions etc.
+\RequirePackage[style=ieee,backend=biber]{biblatex} % bibliography, new engine
+\RequirePackage{listings}               % handle inclusion of source code
 \RequirePackage{csquotes}
+\RequirePackage{amssymb,amsthm,amsmath} % load ams-packages for maths
+\RequirePackage{datetime2}              % month names
+\RequirePackage{titlecaps}              % month names
 
-%% Macro for hiding the chapter texts
-\ifthenelse{\equal{\utuftthesis@hidechapters}{true}}{
-  \renewcommand{\@chapapp}{\relax}
-  \renewcommand{\@makechapterhead}[1]{%
-    \vspace*{50\p@}%
-    {\parindent \z@ \raggedright \normalfont
-      \ifnum \c@secnumdepth >\m@ne
-          \huge\bfseries \@chapapp\space \thechapter\space\space
-      \fi
-      \interlinepenalty\@M
-      \Huge \bfseries #1\par\nobreak
-      \vskip 40\p@
-    }}
-}
-{}
-
-%% load ams-packages for maths
-\RequirePackage{amssymb,amsthm,amsmath}
-
-%% load graphicx package
-%   - automagically select proper parameters depending on whether
-%     we're running pdflatex or latex
-%   - specify \includegraphics{file} without the file extension
-%     (.eps /.pdf (/ .png / .jpeg / ...)), tex should select the proper file
-%
-\RequirePackage{ifpdf}
-\RequirePackage{graphicx}
-
-%% JM: this note refers to some pre-historic latex distributions, probably
-%% safe to disregard
-%% 
-%% !!! NOTE: if you have ancient LaTeX distribution then you might need to
-%% use the following instead
-%% \newif\ifpdf
-%% \ifx\pdfoutput\undefined
-%%   \pdffalse
-%% \else
-%%   \pdfoutput=1
-%%   \pdftrue
-%% \fi
-%% % if graphicx complains about option clash remove the [pdftex] option
-%% \ifpdf
-%%   \RequirePackage[pdftex]{graphicx}
-%% \else
-%%   \RequirePackage[dvips]{graphicx}
-%% \fi
-
-\RequirePackage[style=ieee,backend=biber]{biblatex}
-
-%% Define new title texts for the bibliography
-\langfin
-{\newcommand{\refs}{Lähdeluettelo}}
-{\newcommand{\refs}{References}}
-
-%% Modify the \printbibliography command
-\defbibheading{bibliography}{
-%\eofpages
-% label here if we don't include references
-\ifthenelse{\equal{\utuftthesis@includereferences}{true}}{}{\label{endofpages}}
-\chapter*{\refs}
-\addcontentsline{toc}{chapter}{\refs}}
-
-\let\oldprintbibliography\printbibliography
-\renewcommand{\printbibliography}{
-\oldprintbibliography
-% label here if we also include references
-\ifthenelse{\equal{\utuftthesis@includereferences}{true}}{\label{endofpages}}{}
-}
-
-%% load sverb package
-%   - enhanced handling of verbatim stuff; listing environment
-\RequirePackage{sverb}
-
-%% load listings package
-%   - handle inclusion of source code
-\RequirePackage{listings}
-
-%% load fancyheaders package
-%   - the actual headers and footers are set later
-\RequirePackage{fancyhdr}
+%% switch-case used to select the thesis type
+\newbool{default}
+\newcommand{\case}{}
+\newcommand{\default}{}
+\newenvironment{switch}[1]{%
+    \booltrue{default}
+    \renewcommand{\case}[2]{\ifthenelse{\equal{#1}{##1}}{%
+        \boolfalse{default}##2}{}}%
+    \renewcommand{\default}[1]{\ifbool{default}{##1}{}}
+}{}
 
 %% these are used as global variables on the magic pages
-%% feel free to modify if/when the department changes again
+%% feel free to modify for other languages / if the info is outdated
 \newcommand{\ye@r}{\the\year}
 \newcommand{\m@nth}{\the\month}
-\newcommand{\s@pervisors}{}
-\newcommand{\version}{\utuftthesis@version}
-\newcommand\keywords[1]{\def\k@ywords{#1}}
-\newcommand\keywordstwo[1]{\def\k@ywordstwo{#1}}
+\newcommand{\p@blab}{Labra}
+\newcommand{\p@blaben}{Laboratory}
+\newcommand{\s@pervisors}{none}
+\newcommand{\k@ywords}{not set}
+\newcommand{\k@ywordstwo}{not set}
+\ifdefstring{\utuftthesis@version}{draft}{\newcommand{\version}{[draft]}}{\newcommand{\version}{}}
+\newcommand\keywords[1]{\renewcommand\k@ywords{#1}}
+\newcommand\keywordstwo[1]{\renewcommand\k@ywordstwo{#1}}
 \newcommand\pubyear[1]{\renewcommand\ye@r{#1}}
 \newcommand\pubmonth[1]{\renewcommand\m@nth{#1}}
-\newcommand\publab[2]{\newcommand\p@blab{#1}\newcommand\p@blaben{#2}}
+\newcommand\publab[1]{\renewcommand\p@blab{#1}}
+\newcommand\publaben[1]{\renewcommand\p@blaben{#1}}
 \newcommand\pubtype[1]{\newcommand\p@btype{#1}}
 \newcommand\supervisors[1]{\renewcommand\s@pervisors{#1}}
-\newcommand{\laitos}{Tulevaisuuden teknologioiden laitos}
-\newcommand{\yliopisto}{Turun yliopisto}
-\newcommand{\department}{Department of Future Technologies}
-\newcommand{\university}{University of Turku}
-
-% switch-case used to select the thesis type
-\newboolean{default}
-\newcommand{\case}{}
-\newcommand{\default}{}
-\newenvironment{switch}[1]{%
-    \setboolean{default}{true}
-    \renewcommand{\case}[2]{\ifthenelse{\equal{#1}{##1}}{%
-        \setboolean{default}{false}##2}{}}%
-    \renewcommand{\default}[1]{\ifthenelse{\boolean{default}}{##1}{}}
-}{}
+\newcommand{\department}[1]{\langchoose{#1}{Tulevaisuuden teknologioiden laitos}{Department of Future Technologies}}
+\newcommand{\university}[1]{\langchoose{#1}{Turun yliopisto}{University of Turku}}
+\newcommand{\University}[1]{\langchoose{#1}{\MakeUppercase{Turun yliopisto}}{\MakeUppercase{University of Turku}}}
+\newcommand{\labname}[1]{\langchoose{#1}{\p@blab}{\p@blaben}}
+\newcommand{\pagesname}[1]{\langchoose{#1}{s.}{p.}}
+\newcommand{\apppagesname}[1]{\langchoose{#1}{liites.}{app. p.}}
+\newcommand{\kwlabel}[1]{\langchoose{#1}{Asiasanat}{Keywords}}
+\newcommand{\supervisorstext}{\langfin{Tarkastajat}{Supervisors}}
+\newcommand{\runagain}[1]{\langchoose{#1}\langfin{aja LaTeX uudestaan}{please rerun LaTeX}}
+\newcommand{\turnitin}{\langfin{Turun yliopiston laatujärjestelmän mukaisesti tämän julkaisun alkuperäisyys on tarkastettu Turnitin OriginalityCheck-järjestelmällä.}
+{The originality of this thesis has been checked in accordance with the University of Turku quality assurance system using the Turnitin OriginalityCheck service.}}
+
+%% needs to be implemented like this due to the initialization order
+\langfin{
+\newcommand{\refs}{Lähdeluettelo}
+\newcommand{\appname}{Liitteet}
+}{
+\newcommand{\refs}{References}
+\newcommand{\appname}{Appendices}
+}
 
 %% magic thesis types. more can be added
-\newcommand*{\thesistype}[1]{ \begin{switch}{\p@btype}
-      \case{tkk}{\langchoose{#1}{TkK -tutkielma}{Bachelor's Thesis}}%
-      \case{luk}{\langchoose{#1}{LuK -tutkielma}{Bachelor's Thesis}}%
-      \case{gradu}{\langchoose{#1}{Pro gradu -tutkielma}{Master of Science Thesis}}%
-      \case{di}{\langchoose{#1}{Diplomityö}{Master of Science Thesis}}%
+\newcommand*{\thesistype}[1]{
+  \begin{switch}{\p@btype}
+    \case{tkk}{\langchoose{#1}{TkK -tutkielma}{Bachelor's Thesis}}%
+    \case{luk}{\langchoose{#1}{LuK -tutkielma}{Bachelor's Thesis}}%
+    \case{gradu}{\langchoose{#1}{Pro gradu -tutkielma}{Master of Science Thesis}}%
+    \case{di}{\langchoose{#1}{Diplomityö}{Master of Science Thesis}}%
     \default{\p@btype}%
-    \end{switch}}
+  \end{switch}}
+
+\newcommand{\monthname}[1]{\csname DTM#1monthname\endcsname}
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
@@ -245,7 +217,7 @@
 \addtolength\textwidth{-60 mm}
 \setlength{\oddsidemargin}{-1 in}
 \addtolength\oddsidemargin{40 mm}
-%
+
 % page height... textheight starts at ~4cm from top, 
 % i.e. 1 in + 12 pt + 24 pt = 1.5 in 
 %
@@ -265,147 +237,111 @@
 % - fake 2 linespacing
 %
 \renewcommand{\baselinestretch}{1.655}
-
-%% this way the user can choose whatever date library for other stuff
-\newcommand{\MONTH}[1]{%
-  \ifcase#1
-  \or January% 1
-  \or February% 2
-  \or March% 3
-  \or April% 4
-  \or May% 5
-  \or June% 6
-  \or July% 7
-  \or August% 8
-  \or September% 9
-  \or October% 10
-  \or November% 11
-  \or December% 12
-  \fi}
-\newcommand{\KUUKAUSI}[1]{%
-  \ifcase#1
-  \or Tammikuu% 1
-  \or Helmikuu% 2
-  \or Maaliskuu% 3
-  \or Huhtikuu% 4
-  \or Toukokuu% 5
-  \or Kesäkuu% 6
-  \or Heinäkuu%7
-  \or Elokuu% 8
-  \or Syyskuu% 9
-  \or Lokakuu% 10
-  \or Marraskuu% 11
-  \or Joulukuu% 12
-  \fi}
   
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-% provide the entry environment
-%
+%% Modify the \printbibliography command
+\defbibheading{bibliography}{
+  %\eofpages
+  % label here if we don't include references
+  \ifutuftthesis@includereferences
+  \else
+  \label{endofpages}
+  \fi
+  \chapter*{\refs}
+  \addcontentsline{toc}{chapter}{\refs}
+}
+
+\let\oldprintbibliography\printbibliography
 
-\newenvironment{entry}[1]
-{\begin{list}{}{%
-      \renewcommand{\makelabel}[1]{\textrm{##1:}\hfil}%
-      \settowidth{\labelwidth}{\textrm{#1:}}%
-      \setlength{\leftmargin}{\labelwidth}%
-      \addtolength{\leftmargin}{\labelsep}}}
+\renewcommand{\printbibliography}{
+  \oldprintbibliography
+  % label here if we also include references
+  \ifutuftthesis@includereferences
+    \label{endofpages}
+  \fi
+\langfin{
+  \selectlanguage{finnish}
+}{
+  \selectlanguage{english}
+}
+}
+
+%% Macro for hiding the chapter texts
+\ifutuftthesis@hidechapters
+  \renewcommand{\@chapapp}{\relax}
+  \renewcommand{\@makechapterhead}[1]{%
+    \vspace*{50\p@}%
+    {\parindent \z@ \raggedright \normalfont
+      \ifnum \c@secnumdepth >\m@ne
+          \huge\bfseries \@chapapp\space \thechapter\space\space
+      \fi
+      \interlinepenalty\@M
+      \Huge \bfseries #1\par\nobreak
+      \vskip 40\p@
+    }}
+\fi
+
+%% entry environment
+\newenvironment{entry}[1]{\begin{list}{}{%
+    \renewcommand{\makelabel}[1]{\textrm{##1:}\hfil}%
+    \settowidth{\labelwidth}{\textrm{#1:}}%
+    \setlength{\leftmargin}{\labelwidth}%
+    \addtolength{\leftmargin}{\labelsep}}}
 {\end{list}}
 
 
-%% appendix title, abstract environments
+%% the first abstract
 \langfin{
-  \newcommand{\appname}{Liitteet}
   \renewenvironment{abstract}{\@bstracth{finnish}}{\@bstractt{finnish}{\k@ywords}}
+
+  %% the second abstract environment
+  %% always in english - but can be omitted if the document uses a single language
+  \newenvironment{abstracten}{\@bstracth{english}}{\@bstractt{english}{\k@ywordstwo}}
 }{
-  \newcommand{\appname}{Appendices}
   \renewenvironment{abstract}{\@bstracth{english}}{\@bstractt{english}{\k@ywords}}
 }
 
-%% used for listing the supervisors
-\RequirePackage{pgffor}
-
 %% title page, reuses the standard \maketitle
 \renewcommand*{\maketitle}{%
-\clearpage 
-\thispagestyle{empty}
-\vspace*{\stretch{1}}
-\vspace{3mm}
-\noindent 
-\rule{\linewidth}{1 mm}
-\renewcommand{\baselinestretch}{1.05}\normalsize
-\begin{flushright}
-\Huge{\@title}
-\end{flushright}
-\rule{\linewidth}{1 mm}
-\vspace*{\stretch{4}}
-\\
-\renewcommand{\baselinestretch}{1}\normalsize
- \iffin
- \begin{tabular}{p{80 mm}p{70 mm}}           \\
- & \thesistype{\n@tivelang} \\
- \else
-\begin{tabular}{p{78 mm}p{72 mm}}            \\
- & \thesistype{english}                              \\
- \fi
- & \langfin{\yliopisto}{\university}   \\
- & \langfin{\laitos}{\department}      \\
- & \langfin{\p@blab}{\p@blaben}        \\
- & \ye@r                               \\
- & \@author                            \\
-\ifthenelse{\equal{\s@pervisors}{}}{
-}{
- &                                           \\
- & \langfin{Tarkastajat}{Supervisors}:                              \\ 
- \foreach \x in \s@pervisors {
-\begin{tabular}{p{78 mm}p{72 mm}}            
- & \hspace*{4 ex} \x         \\
-\end{tabular}
-}}
-\end{tabular}
-\vspace*{\stretch{1}}\\
-\begin{minipage}[b]{\textwidth}
-\scriptsize \langfin{{\yliopisto}n laatujärjestelmän mukaisesti tämän julkaisun alkuperäisyys on tarkastettu Turnitin OriginalityCheck-järjestelmällä.}{
-The originality of this thesis has been checked in
-accordance with the \university quality assurance system using
-the Turnitin OriginalityCheck service.}
-\end{minipage}
-\renewcommand{\baselinestretch}{1.655}\normalsize
-\clearpage
-}
+  \clearpage 
+  \thispagestyle{empty}
+  \vspace*{\stretch{1}}
+  \vspace{3mm}
+  \noindent 
+  \rule{\linewidth}{1 mm}
+  \renewcommand{\baselinestretch}{1.05}\normalsize
+  \begin{flushright}
+  \Huge{\@title}
+  \end{flushright}
+  \rule{\linewidth}{1 mm}
+  \vspace*{\stretch{4}}
 
-%% used to detect the number of pages
-\RequirePackage[page]{totalcount}
+  \renewcommand{\baselinestretch}{1}\normalsize
+  \begin{adjustbox}{minipage=65mm,right}
+  \thesistype{\l@nguage}\\
+  \university{\l@nguage}\\
+  \department{\l@nguage}\\
+  \labname{\l@nguage}\\
+  \ye@r\\
+  \@author
+  \ifdefstring{\s@pervisors}{none}{}{\\
+    \\
+    \supervisorstext:
+    \expandafter\forcsvlist\expandafter\\\expandafter{\s@pervisors}
+  }
+  \end{adjustbox}
+  \vspace*{\stretch{1}}\\
+  \scriptsize \turnitin
+  
+  \renewcommand{\baselinestretch}{1.655}\normalsize
+  \clearpage
+}
 
 %% used to detect the empty \totalpages on the first latex run
-\newcommand*{\IsInteger}[3]{%
-    \IfStrEq{#1}{ }{%
-        %#3% is a blank string
-    }{%
-        \IfInteger{#1}{#2}{#3}%
-    }%
-}%
-
-\RequirePackage{xstring}
-  
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-% define itabstract environment for abstract page
-%
+\newcommand*{\IsInteger}[3]{\IfStrEq{#1}{ }{}{\IfInteger{#1}{#2}{#3}}}
+
+%% top part of the abstract page (since we use environments)
 \newcommand{\@bstracth}[1]{%
   \selectlanguage{#1}
-  \langchoose{#1}{
-    \newcommand{\uni}{\yliopisto}
-    \newcommand{\dep}{\laitos}
-    \newcommand{\@pp}{s.}
-    \newcommand{\@app}{liites.}
-  }
-  {
-    \newcommand{\uni}{\university}
-    \newcommand{\dep}{\department}
-    \newcommand{\@pp}{p.}
-    \newcommand{\@app}{app. p.}
-  }
-  
   \setlength{\headheight}{0 pt}
   \setlength{\headsep}{0 pt}
   \addtolength\topmargin{-0.54 cm}
@@ -414,112 +350,115 @@ the Turnitin OriginalityCheck service.}
   \thispagestyle{empty}
   \noindent
   \begin{minipage}[\textheight]{\linewidth}
-  \MakeUppercase{\uni}\\
-  \dep
+  \University{#1}\\
+  \department{#1}
   \begin{entry}{}
   \item[\textsc{\@author}] \@title
   \end{entry}
-\IsInteger{\totalpages}{
-\ifnum \totalpages > 0
-  \thesistype{#1}, \pageref{endofpages} \@pp, \totalpages\ \@app \\
-\else
-  \thesistype{#1}, \pageref{endofpages} \@pp \\
-\fi
-}
-{\thesistype{#1}, \langfin{aja LaTeX uudestaan}{please rerun LaTeX} \\
-}
-  \langchoose{#1}{\p@blab}{\p@blaben}\\
-  \langchoose{#1}{\KUUKAUSI{\m@nth}}{\MONTH{\m@nth}} \ye@r \\
+  \thesistype{#1},
+  \IsInteger{\totalpages}{
+    \pageref{endofpages} \pagesname{#1}%
+    \ifnum \totalpages > 0
+      , \totalpages\ \apppagesname{#1} \\
+    \else
+      \\
+    \fi
+  }{ \runagain{#1} \\
+  }
+  \labname{#1}\\
+  \titlecap{\monthname{#1}{\m@nth}} \ye@r\\
   \rule{\linewidth}{1 pt}
   \\
 }
 
+%% bottom part of the abstract page
 \newcommand{\@bstractt}[2]{%
-\bigskip
-\\
-\selectlanguage{#1}
-\newcommand{\kwlabel}{\langchoose{#1}{Asiasanat}{Keywords}}
-\begin{entry}{}
-\item[\kwlabel] #2
-\end{entry}
-\end{minipage}
-\clearpage
-\setcounter{page}{0}
-\setlength{\headheight}{12 pt}
-\setlength{\headsep}{24 pt}
-\setlength{\topmargin}{0 mm}
-\renewcommand{\baselinestretch}{1.655}\normalsize
+  \selectlanguage{#1}
+  \bigskip
+  \\
+  \begin{entry}{}
+    \item[\kwlabel{#1}] #2
+  \end{entry}
+  \end{minipage}
+  \clearpage
+  \setcounter{page}{0}
+  \setlength{\headheight}{12 pt}
+  \setlength{\headsep}{24 pt}
+  \setlength{\topmargin}{0 mm}
+  \renewcommand{\baselinestretch}{1.655}\normalsize
+  
+  % roman numbering for table of contents etc.
+  \pagenumbering{roman}
 }
 
-%% the second abstract environment
-\newenvironment{abstracten}{\@bstracth{english}}{\@bstractt{english}{\k@ywordstwo}}
+%% define \figcaption and \tblcaption for use with non-floating figures and tables
+\newcommand\figcaption{\def\@captype{figure}\caption}
+\newcommand\tblcaption{\def\@captype{table}\caption}
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% 
-% define \figcaption and \tblcaption for use with non-floating figures and 
-% tables
-%
-
-\makeatletter
-  \newcommand\figcaption{\def\@captype{figure}\caption}
-  \newcommand\tblcaption{\def\@captype{table}\caption}
-\makeatother
+%% compatible with old versions that required using \startpages
+%% now optional
+\newcommand{\startpages}{\initdocstyle}
 
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-% define \startpages
-%
-\newcommand{\startpages}{%
-        \clearpage
-        \pagenumbering{arabic}
-        \setcounter{page}{1}
-        \renewcommand{\baselinestretch}{1.655}
-
-        \pagestyle{fancy}
-        \renewcommand{\chaptermark}[1]{
-          \markboth{
-            \MakeUppercase{\chaptername\ \thechapter.\ ##1}}
-          {}
-        }
-        \lhead{\leftmark}
-
-\chead{\ifthenelse{\equal{draft}{\version}}{[draft]}{}}
-        \rhead{\thepage}
-        \fancyfoot{}
+%% initializes the settings for main document content
+\newcommand{\initdocstyle}{%
+  \renewcommand{\startpages}{}
+\langfin{
+  \selectlanguage{finnish}
+}{
+  \selectlanguage{english}
+}
+  \clearpage
+  % clear 1st page headers/footers per request
+  \ifutuftthesis@emptyfirstpages
+  \fancypagestyle{plain}{\fancyhf{}\renewcommand{\headrulewidth}{0 pt}}
+  \fi
+  \pagenumbering{arabic}
+  \setcounter{page}{1}
+  \renewcommand{\baselinestretch}{1.655}
+  \pagestyle{fancy}
+  \renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{\chaptername\ \thechapter.\ ##1}}{}}
+  \lhead{\leftmark}
+  \chead{\version}
+  \rhead{\thepage}
+  \fancyfoot{}
+  % the totalcount counter is only used for appendices, set a bogus value now
+  % fails if the document has over 1000 pages
   \setcounter{totalcount@page}{-1000}
 }       
 
+%% postpone the doc style through these sections
+\pretocmd{\tableofcontents}{\renewcommand{\startpages}{}}{}{}
+\apptocmd{\tableofcontents}{\renewcommand{\startpages}{\initdocstyle}}{}{}
+\pretocmd{\listoffigures}{\renewcommand{\startpages}{}}{}{}
+\apptocmd{\listoffigures}{\renewcommand{\startpages}{\initdocstyle}}{}{}
+\pretocmd{\listoftables}{\renewcommand{\startpages}{}}{}{}
+\apptocmd{\listoftables}{\renewcommand{\startpages}{\initdocstyle}}{}{}
 
+%% finally initialize the main document style
+\pretocmd{\chapter}{\startpages}{}{}
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-% define \appendices
-%
-\newcommand{\appendices}{%
-  \newpage
+%% compatible with old versions that required using \appendices
+%% now optional
+\newcommand{\appendices}{\startappendices}
+
+%% initializes the settings for appendices
+\newcommand{\startappendices}{%
+  \renewcommand{\appendices}{}
+  \clearpage
   \appendix
   \addtocontents{toc}{\protect\contentsline{chapter}{\appname}{}\relax}
   \renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{\appendixname\ \thechapter.\ ##1}}{}}
-  \lhead{\leftmark}
-  \chead{}
   \rhead{\thechapter-\arabic{page}}
-  \fancyfoot{}
   \setcounter{page}{0}
   \renewcommand\thepage{\thechapter-\arabic{page}}
-  \setcounter{totalcount@page}{-1}
+  \setcounter{totalcount@page}{0}
 }
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-% define \appchapter
-%
+%% use in place of \chapter
 \newcommand{\appchapter}[1]{%
-        \addtocounter{totalcount@page}{\value{page}}
-        \clearpage
-        \setcounter{page}{1}
-        \chapter{#1}
+  \appendices
+  \addtocounter{totalcount@page}{\value{page}}
+  \clearpage
+  \setcounter{page}{1}
+  \chapter{#1}
 }
-
-%% automatically handle the appendix magic
-%\AtEndDocument{\addtocounter{totalcount@page}{-2}}
diff --git a/latex/utuftthesis.layout b/latex/utuftthesis.layout
index ccfca8d411523e6c46de19ef7b86369e90d380f0..da0a209d6a784bc14a783f640d4a21c67d407a64 100644
--- a/latex/utuftthesis.layout
+++ b/latex/utuftthesis.layout
@@ -62,6 +62,18 @@ Style PubLab
     EndFont
 End
 
+Style PubLabEN
+    Margin                Dynamic
+    LatexType             Command
+    LatexName             publaben
+    LabelType   Static
+    LabelString           "Laboratory name (for 2nd abstract): "
+    Align                 Center
+    Font
+      Size                Large
+    EndFont
+End
+
 Style PubType
     Margin                Dynamic
     LatexType             Command
diff --git a/tests/Bibliografia.bib b/tests/Bibliografia.bib
new file mode 100644
index 0000000000000000000000000000000000000000..82c68870729f48a19d6ca0bf1509fa119ee31d3f
--- /dev/null
+++ b/tests/Bibliografia.bib
@@ -0,0 +1,17 @@
+@article{crawley2007write,
+  title={How to Write a Research Proposal and Succeed},
+  author={Crawley, Gerard M and O'Sullivan, Eoin},
+  year={2007},
+  publisher={World Scientific}
+}
+
+@article{puasuareanu2009survey,
+  title={A survey of new trends in symbolic execution for software testing and analysis},
+  author={P{\u{a}}s{\u{a}}reanu, Corina S and Visser, Willem},
+  journal={International journal on software tools for technology transfer},
+  volume={11},
+  number={4},
+  pages={339--353},
+  year={2009},
+  publisher={Springer}
+}
\ No newline at end of file
diff --git a/tests/test-app1.tex b/tests/test-app1.tex
new file mode 100644
index 0000000000000000000000000000000000000000..1b85d6d8e1e9b35bf66678049a2fea1365f7d852
--- /dev/null
+++ b/tests/test-app1.tex
@@ -0,0 +1,52 @@
+% test document for gitlab ci tests
+
+\documentclass[a4paper,12pt,language=finnish]{utuftthesis}
+
+% how many levels of section numbering
+\setcounter{secnumdepth}{2}
+
+% how many levels of section numbering in the TOC
+\setcounter{tocdepth}{2}
+
+\usepackage{blindtext}
+% suppress complaints about missing finnish blabla
+\pretocmd{\blindtext}{\selectlanguage{english}}{}{}
+\pretocmd{\Blindtext}{\selectlanguage{english}}{}{}
+
+\addbibresource{Bibliografia.bib}
+
+\pubyear{2018}
+\pubmonth{6}
+\publab{Labran nimi}
+\pubtype{tkk}
+\title{Tutkielma}
+\author{Oma nimi}
+\keywords{tähän, lista, avainsanoista}
+
+\begin{document}
+\maketitle
+\begin{abstract}\blindtext\end{abstract}
+
+\tableofcontents
+
+\chapter{Johdanto}
+\blindtext
+Viitataan tähän \cite{crawley2007write}.
+
+\chapter{Lopuksi}
+\blindtext
+
+\printbibliography
+
+\appchapter{Liitedokumentti}
+sivu 1
+
+\appchapter{Liitedokumentti 2}
+sivu 1
+\newpage
+sivu 2
+
+\appchapter{Liitedokumentti 3}
+\Blindtext
+
+\end{document}
diff --git a/tests/test-app2.tex b/tests/test-app2.tex
new file mode 100644
index 0000000000000000000000000000000000000000..0a44773dcccf309c1f3899f5c68d9491f501cbd9
--- /dev/null
+++ b/tests/test-app2.tex
@@ -0,0 +1,49 @@
+% test document for gitlab ci tests
+
+\documentclass[a4paper,12pt,language=finnish]{utuftthesis}
+
+% how many levels of section numbering
+\setcounter{secnumdepth}{2}
+
+% how many levels of section numbering in the TOC
+\setcounter{tocdepth}{2}
+
+\usepackage{blindtext}
+% suppress complaints about missing finnish blabla
+\pretocmd{\blindtext}{\selectlanguage{english}}{}{}
+\pretocmd{\Blindtext}{\selectlanguage{english}}{}{}
+
+\addbibresource{Bibliografia.bib}
+
+\pubyear{2018}
+\pubmonth{6}
+\publab{Labran nimi}
+\pubtype{tkk}
+\title{Tutkielma}
+\author{Oma nimi}
+\keywords{tähän, lista, avainsanoista}
+
+\begin{document}
+\maketitle
+\begin{abstract}\blindtext\end{abstract}
+
+\tableofcontents
+
+\chapter{Johdanto}
+\blindtext
+Viitataan tähän \cite{crawley2007write}.
+
+\chapter{Lopuksi}
+\blindtext
+
+\printbibliography
+
+\appchapter{Liitedokumentti}
+sivu 1
+
+\appchapter{Liitedokumentti 2}
+sivu 1
+\newpage
+sivu 2
+
+\end{document}
diff --git a/tests/test-app3.tex b/tests/test-app3.tex
new file mode 100644
index 0000000000000000000000000000000000000000..0387c197395fbfd16ccb8c3491707c9b0c4888c3
--- /dev/null
+++ b/tests/test-app3.tex
@@ -0,0 +1,44 @@
+% test document for gitlab ci tests
+
+\documentclass[a4paper,12pt,language=finnish]{utuftthesis}
+
+% how many levels of section numbering
+\setcounter{secnumdepth}{2}
+
+% how many levels of section numbering in the TOC
+\setcounter{tocdepth}{2}
+
+\usepackage{blindtext}
+% suppress complaints about missing finnish blabla
+\pretocmd{\blindtext}{\selectlanguage{english}}{}{}
+\pretocmd{\Blindtext}{\selectlanguage{english}}{}{}
+
+\addbibresource{Bibliografia.bib}
+
+\pubyear{2018}
+\pubmonth{6}
+\publab{Labran nimi}
+\pubtype{tkk}
+\title{Tutkielma}
+\author{Oma nimi}
+\keywords{tähän, lista, avainsanoista}
+
+\begin{document}
+\maketitle
+\begin{abstract}\blindtext\end{abstract}
+
+\tableofcontents
+
+\chapter{Johdanto}
+\blindtext
+Viitataan tähän \cite{crawley2007write}.
+
+\chapter{Lopuksi}
+\blindtext
+
+\printbibliography
+
+\appchapter{Liitedokumentti}
+sivu 1
+
+\end{document}
diff --git a/tests/test-app4.tex b/tests/test-app4.tex
new file mode 100644
index 0000000000000000000000000000000000000000..8460caf7ec44cd8b458af3d8013fda6fbbff4d1a
--- /dev/null
+++ b/tests/test-app4.tex
@@ -0,0 +1,41 @@
+% test document for gitlab ci tests
+
+\documentclass[a4paper,12pt,language=finnish]{utuftthesis}
+
+% how many levels of section numbering
+\setcounter{secnumdepth}{2}
+
+% how many levels of section numbering in the TOC
+\setcounter{tocdepth}{2}
+
+\usepackage{blindtext}
+% suppress complaints about missing finnish blabla
+\pretocmd{\blindtext}{\selectlanguage{english}}{}{}
+\pretocmd{\Blindtext}{\selectlanguage{english}}{}{}
+
+\addbibresource{Bibliografia.bib}
+
+\pubyear{2018}
+\pubmonth{6}
+\publab{Labran nimi}
+\pubtype{tkk}
+\title{Tutkielma}
+\author{Oma nimi}
+\keywords{tähän, lista, avainsanoista}
+
+\begin{document}
+\maketitle
+\begin{abstract}\blindtext\end{abstract}
+
+\tableofcontents
+
+\chapter{Johdanto}
+\blindtext
+Viitataan tähän \cite{crawley2007write}.
+
+\chapter{Lopuksi}
+\blindtext
+
+\printbibliography
+
+\end{document}
diff --git a/tests/test-bilingual.tex b/tests/test-bilingual.tex
new file mode 100644
index 0000000000000000000000000000000000000000..901a6a6dd1544c0110c683447ae7a7f00bc115bd
--- /dev/null
+++ b/tests/test-bilingual.tex
@@ -0,0 +1,74 @@
+% test document for gitlab ci tests
+
+\documentclass[a4paper,12pt,language=finnish]{utuftthesis}
+
+% how many levels of section numbering
+\setcounter{secnumdepth}{2}
+
+% how many levels of section numbering in the TOC
+\setcounter{tocdepth}{2}
+
+\usepackage{blindtext}
+% suppress complaints about missing finnish blabla
+\pretocmd{\blindtext}{\selectlanguage{english}}{}{}
+\pretocmd{\Blindtext}{\selectlanguage{english}}{}{}
+
+\addbibresource{Bibliografia.bib}
+
+\pubyear{2018}
+\pubmonth{6}
+\publab{Labran nimi}
+\publaben{Laboratory Name}
+\pubtype{tkk}
+\supervisors{Sauronin silmä, Kalle Yylä, P. Rofessori}
+\title{Name of Thesis}
+\author{My Name}
+\keywords{tähän, lista, avainsanoista}
+\keywordstwo{here, a, list, of, keywords}
+
+\begin{document}
+\maketitle
+\begin{abstract}\blindtext\end{abstract}
+\begin{abstracten}\blindtext\end{abstracten}
+
+\tableofcontents
+\listoffigures
+\listoftables
+
+\chapter{Johdanto}
+\Blindtext
+Viitataan tähän \cite{crawley2007write}.
+
+\chapter{Väliluku 1}
+\Blindtext
+
+\chapter{Väliluku 2}
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 3}
+\section{foobar}
+\Blindtext
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 4}
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 5}
+\Blindtext
+
+\chapter{Väliluku 6}
+\Blindtext
+
+\chapter{Lopuksi}
+
+\blindtext
+
+\printbibliography
+
+\appchapter{Liitedokumentti}
+\Blindtext
+
+\end{document}
diff --git a/tests/test-english.tex b/tests/test-english.tex
new file mode 100644
index 0000000000000000000000000000000000000000..5c909df932d643f631f15e424171757b4ca5947c
--- /dev/null
+++ b/tests/test-english.tex
@@ -0,0 +1,71 @@
+% test document for gitlab ci tests
+
+\documentclass[a4paper,12pt,language=english]{utuftthesis}
+
+% how many levels of section numbering
+\setcounter{secnumdepth}{2}
+
+% how many levels of section numbering in the TOC
+\setcounter{tocdepth}{2}
+
+\usepackage{blindtext}
+% suppress complaints about missing finnish blabla
+\pretocmd{\blindtext}{\selectlanguage{english}}{}{}
+\pretocmd{\Blindtext}{\selectlanguage{english}}{}{}
+
+\addbibresource{Bibliografia.bib}
+
+\pubyear{2018}
+\pubmonth{6}
+\publab{Laboratory Name}
+\pubtype{tkk}
+\supervisors{aa 123,bb 234,cc 345}
+\title{Name of Thesis}
+\author{My Name}
+\keywords{here, a, list, of, keywords}
+
+\begin{document}
+\maketitle
+\begin{abstract}\blindtext\end{abstract}
+
+\tableofcontents
+\listoffigures
+\listoftables
+
+\chapter{Johdanto}
+\Blindtext
+Viitataan tähän \cite{crawley2007write}.
+
+\chapter{Väliluku 1}
+\Blindtext
+
+\chapter{Väliluku 2}
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 3}
+\section{foobar}
+\Blindtext
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 4}
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 5}
+\Blindtext
+
+\chapter{Väliluku 6}
+\Blindtext
+
+\chapter{Lopuksi}
+
+\blindtext
+
+\printbibliography
+
+\appchapter{Liitedokumentti}
+\Blindtext
+
+\end{document}
diff --git a/tests/test-finnish.tex b/tests/test-finnish.tex
new file mode 100644
index 0000000000000000000000000000000000000000..8944aed272903fcb1e3128af33756aff0e440eab
--- /dev/null
+++ b/tests/test-finnish.tex
@@ -0,0 +1,71 @@
+% test document for gitlab ci tests
+
+\documentclass[a4paper,12pt,language=finnish]{utuftthesis}
+
+% how many levels of section numbering
+\setcounter{secnumdepth}{2}
+
+% how many levels of section numbering in the TOC
+\setcounter{tocdepth}{2}
+
+\usepackage{blindtext}
+% suppress complaints about missing finnish blabla
+\pretocmd{\blindtext}{\selectlanguage{english}}{}{}
+\pretocmd{\Blindtext}{\selectlanguage{english}}{}{}
+
+\addbibresource{Bibliografia.bib}
+
+\pubyear{2018}
+\pubmonth{6}
+\publab{Labran nimi}
+\pubtype{tkk}
+\supervisors{Sauronin silmä, Kalle Yylä, P. Rofessori}
+\title{Tutkielma}
+\author{Oma nimi}
+\keywords{tähän, lista, avainsanoista}
+
+\begin{document}
+\maketitle
+\begin{abstract}\blindtext\end{abstract}
+
+\tableofcontents
+\listoffigures
+\listoftables
+
+\chapter{Johdanto}
+\Blindtext
+Viitataan tähän \cite{crawley2007write}.
+
+\chapter{Väliluku 1}
+\Blindtext
+
+\chapter{Väliluku 2}
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 3}
+\section{foobar}
+\Blindtext
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 4}
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 5}
+\Blindtext
+
+\chapter{Väliluku 6}
+\Blindtext
+
+\chapter{Lopuksi}
+
+\blindtext
+
+\printbibliography
+
+\appchapter{Liitedokumentti}
+\Blindtext
+
+\end{document}
diff --git a/tests/test-hyperref.tex b/tests/test-hyperref.tex
new file mode 100644
index 0000000000000000000000000000000000000000..f574fa8d809f67e3cc19917519ad1be621d53632
--- /dev/null
+++ b/tests/test-hyperref.tex
@@ -0,0 +1,76 @@
+% test document for gitlab ci tests
+
+\documentclass[a4paper,12pt,language=finnish]{utuftthesis}
+
+% how many levels of section numbering
+\setcounter{secnumdepth}{2}
+
+% how many levels of section numbering in the TOC
+\setcounter{tocdepth}{2}
+
+\usepackage{hyperref}
+
+\usepackage{blindtext}
+% suppress complaints about missing finnish blabla
+\pretocmd{\blindtext}{\selectlanguage{english}}{}{}
+\pretocmd{\Blindtext}{\selectlanguage{english}}{}{}
+
+\addbibresource{Bibliografia.bib}
+
+\pubyear{2018}
+\pubmonth{6}
+\publab{Labran nimi}
+\publaben{Laboratory Name}
+\pubtype{tkk}
+\supervisors{Sauronin silmä, Kalle Yylä, P. Rofessori}
+\title{Name of Thesis}
+\author{My Name}
+\keywords{tähän, lista, avainsanoista}
+\keywordstwo{here, a, list, of, keywords}
+
+\begin{document}
+\maketitle
+\begin{abstract}\blindtext\end{abstract}
+\begin{abstracten}\blindtext\end{abstracten}
+
+\tableofcontents
+\listoffigures
+\listoftables
+
+\chapter{Johdanto}
+\Blindtext
+Viitataan tähän \cite{crawley2007write}.
+
+\chapter{Väliluku 1}
+\Blindtext
+
+\chapter{Väliluku 2}
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 3}
+\section{foobar}
+\Blindtext
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 4}
+\section{foobar}
+\Blindtext
+
+\chapter{Väliluku 5}
+\Blindtext
+
+\chapter{Väliluku 6}
+\Blindtext
+
+\chapter{Lopuksi}
+
+\blindtext
+
+\printbibliography
+
+\appchapter{Liitedokumentti}
+\Blindtext
+
+\end{document}
diff --git a/tests/utuftthesis.cls b/tests/utuftthesis.cls
new file mode 100644
index 0000000000000000000000000000000000000000..13dbc9be3306dc503f7d63b7d00bd8075d8983a8
--- /dev/null
+++ b/tests/utuftthesis.cls
@@ -0,0 +1,464 @@
+% version 2.2 2018/10/06 Bugfixes: Got rid of startappendices and startpages.
+%                                  various fixes. cleanups
+% version 2.1 2018/10/02 Bugfixes: removed redundant tutkielma.tex, fixed issues with
+%                                  appendices, small caps, added an option to enable
+%                                  the truetype times new roman font
+% version 2.0 2018/08/29 new version using document classes, xelatex, biblatex, and
+%                        additional TeX scripting. Jari-Matti Mäkelä (jmjm@iki.fi)
+% version 1.3+ 2017/10/01 - 2016/9/22 additions marked "JH:", modified for use in
+%                                     tex.soft.utu.fi, Johannes Holvitie (jjholv@utu.fi)
+% version 1.3 2015/09/05 Sami Nuuttila (samnuutt@utu.fi)
+
+% ------------------------------
+% How to use? Store the file in
+% 1) Windows XP C:\Documents and Settings\<user name>\texmf\tex\latex\local\
+% 2) Windows Vista/7 C:\Users\<user name>\texmf\tex\latex\local\
+% 3) Linux ~/texmf/tex/latex/local/
+% 4) Mac OS X /Users/<user name>/Library/texmf/tex/latex/local/
+% 5) Sharelatex: just copy this to the project folder
+% 6) or in the same directory as the document
+% ------------------------------
+
+\NeedsTeXFormat{LaTeX2e}[2007/01/01]
+\ProvidesClass{utuftthesis}[2018/10/06 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}           % misc. tools
+\RequirePackage{ifxetex}          % for xelatex/pdflatex font selection
+\RequirePackage{fancyhdr}         % customized headers/footers
+\RequirePackage{xstring}          % string operations (number of pages etc.)
+\RequirePackage[page]{totalcount} % the total number of pages
+\RequirePackage{comment}          % LyX generated comments
+
+%% not used by the class
+% if you encounter package clashes, start by disabling these
+\RequirePackage{graphicx}        % \includegraphics{file}, works without file extensions
+\RequirePackage{sverb}           % enhanced handling of verbatim stuff
+
+%% Custom package options
+%\DeclareStringOption[00]{year}
+\DeclareStringOption[draft]{version}
+\DeclareStringOption[a4paper]{paper}
+\DeclareStringOption[finnish]{language}
+\DeclareBoolOption[true]{hidechapters}
+\DeclareBoolOption[false]{includereferences}
+\DeclareBoolOption[false]{realtimesnewroman}
+\DeclareBoolOption[false]{sharelatex}
+\DeclareBoolOption[true]{emptyfirstpages}
+
+
+%% Process given options
+\ProcessKeyvalOptions*
+
+%% Fallback
+%\DeclareOption*{}
+%  \ClassWarning{myclass}{Unknown option '\CurrentOption'}
+%\ProcessOptions\relax
+%\RequirePackage{ifpdf}           % not used
+%% automatically handle the appendix magic
+%\AtEndDocument{\addtocounter{totalcount@page}{-2}}
+
+%% Load base
+\LoadClass[\utuftthesis@paper,12pt]{report}
+
+%% Some language macros for choosing between content
+%% the langchoose is now only good for \n@tivelang & english. easy to adjust
+%% for swedish & english, but multiple choices requires the use of switch-case
+\newcommand{\n@tivelang}{finnish}
+\newcommand{\l@nguage}{\utuftthesis@language}
+\newcommand{\thedoclanguage}{\utuftthesis@language}
+%\newcommand{\langchoose}[3]{\expandafter\ifdefstrequal\expandafter{#1}{\n@tivelang}{#2}{#3}}
+\newcommand{\langchoose}[3]{\ifthenelse{\equal{#1}{\n@tivelang}}{#2}{#3}}
+\newcommand{\langfin}[2]{\langchoose{\l@nguage}{#1}{#2}}
+
+%% Set up encoding/font/hyphenation stuff
+\ifxetex
+  \RequirePackage{fontspec}
+  \RequirePackage{polyglossia}
+  \setmainlanguage{\utuftthesis@language}
+  \langfin{\setotherlanguage{english}}
+  
+  \ifutuftthesis@realtimesnewroman
+    % can't use (C) TTF fonts when the ms corefonts are not installed
+    \ifutuftthesis@sharelatex
+      \setmainfont[BoldFont=timesbd.ttf,ItalicFont=timesi.ttf,BoldItalicFont=timesbi.ttf]{times.ttf}
+    \else
+      \setmainfont{Times New Roman}
+    \fi
+  \fi
+\fi
+\ifxetex
+\else
+  % JH: modified latin to UTF-8 encoding cues to make Scandinavian characters works
+  \RequirePackage[T1]{fontenc}
+  \RequirePackage[utf8]{inputenc}
+  \RequirePackage{lmodern}
+
+  % load babel-package for automatic hyphenation
+  \ifdefstring{\utuftthesis@language}{english}{
+    \RequirePackage[english]{babel}
+  }{
+    \RequirePackage[english, \l@nguage]{babel}
+  }
+  
+  % change default font to times-roman
+  \renewcommand{\sfdefault}{phv}
+  \renewcommand{\rmdefault}{ptm}
+  \renewcommand{\ttdefault}{pcr}
+\fi
+  
+%% the rest of the requirements listed here as they have
+%% language specific definitions etc.
+\RequirePackage[style=ieee,backend=biber]{biblatex} % bibliography, new engine
+\RequirePackage{listings}               % handle inclusion of source code
+\RequirePackage{csquotes}
+\RequirePackage{amssymb,amsthm,amsmath} % load ams-packages for maths
+\RequirePackage{datetime2}              % month names
+\RequirePackage{titlecaps}              % month names
+
+%% switch-case used to select the thesis type
+\newbool{default}
+\newcommand{\case}{}
+\newcommand{\default}{}
+\newenvironment{switch}[1]{%
+    \booltrue{default}
+    \renewcommand{\case}[2]{\ifthenelse{\equal{#1}{##1}}{%
+        \boolfalse{default}##2}{}}%
+    \renewcommand{\default}[1]{\ifbool{default}{##1}{}}
+}{}
+
+%% these are used as global variables on the magic pages
+%% feel free to modify for other languages / if the info is outdated
+\newcommand{\ye@r}{\the\year}
+\newcommand{\m@nth}{\the\month}
+\newcommand{\p@blab}{Labra}
+\newcommand{\p@blaben}{Laboratory}
+\newcommand{\s@pervisors}{none}
+\newcommand{\k@ywords}{not set}
+\newcommand{\k@ywordstwo}{not set}
+\ifdefstring{\utuftthesis@version}{draft}{\newcommand{\version}{[draft]}}{\newcommand{\version}{}}
+\newcommand\keywords[1]{\renewcommand\k@ywords{#1}}
+\newcommand\keywordstwo[1]{\renewcommand\k@ywordstwo{#1}}
+\newcommand\pubyear[1]{\renewcommand\ye@r{#1}}
+\newcommand\pubmonth[1]{\renewcommand\m@nth{#1}}
+\newcommand\publab[1]{\renewcommand\p@blab{#1}}
+\newcommand\publaben[1]{\renewcommand\p@blaben{#1}}
+\newcommand\pubtype[1]{\newcommand\p@btype{#1}}
+\newcommand\supervisors[1]{\renewcommand\s@pervisors{#1}}
+\newcommand{\department}[1]{\langchoose{#1}{Tulevaisuuden teknologioiden laitos}{Department of Future Technologies}}
+\newcommand{\university}[1]{\langchoose{#1}{Turun yliopisto}{University of Turku}}
+\newcommand{\University}[1]{\langchoose{#1}{\MakeUppercase{Turun yliopisto}}{\MakeUppercase{University of Turku}}}
+\newcommand{\labname}[1]{\langchoose{#1}{\p@blab}{\p@blaben}}
+\newcommand{\pagesname}[1]{\langchoose{#1}{s.}{p.}}
+\newcommand{\apppagesname}[1]{\langchoose{#1}{liites.}{app. p.}}
+\newcommand{\kwlabel}[1]{\langchoose{#1}{Asiasanat}{Keywords}}
+\newcommand{\supervisorstext}{\langfin{Tarkastajat}{Supervisors}}
+\newcommand{\runagain}[1]{\langchoose{#1}\langfin{aja LaTeX uudestaan}{please rerun LaTeX}}
+\newcommand{\turnitin}{\langfin{Turun yliopiston laatujärjestelmän mukaisesti tämän julkaisun alkuperäisyys on tarkastettu Turnitin OriginalityCheck-järjestelmällä.}
+{The originality of this thesis has been checked in accordance with the University of Turku quality assurance system using the Turnitin OriginalityCheck service.}}
+
+%% needs to be implemented like this due to the initialization order
+\langfin{
+\newcommand{\refs}{Lähdeluettelo}
+\newcommand{\appname}{Liitteet}
+}{
+\newcommand{\refs}{References}
+\newcommand{\appname}{Appendices}
+}
+
+%% magic thesis types. more can be added
+\newcommand*{\thesistype}[1]{
+  \begin{switch}{\p@btype}
+    \case{tkk}{\langchoose{#1}{TkK -tutkielma}{Bachelor's Thesis}}%
+    \case{luk}{\langchoose{#1}{LuK -tutkielma}{Bachelor's Thesis}}%
+    \case{gradu}{\langchoose{#1}{Pro gradu -tutkielma}{Master of Science Thesis}}%
+    \case{di}{\langchoose{#1}{Diplomityö}{Master of Science Thesis}}%
+    \default{\p@btype}%
+  \end{switch}}
+
+\newcommand{\monthname}[1]{\csname DTM#1monthname\endcsname}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+% setup document appearance
+%
+
+% fix page size
+%
+%  - page width: 
+%     1inch             +
+%      \hoffset         +
+%      \oddsidemargin   +
+%      \textwidth       +
+%      \marginsep       +
+%      \marginparwidth
+%
+%  - page height: 
+%     1inch             +
+%      \voffset         +
+%      \topmargin       +
+%      \headheight      +
+%      \headsep         +
+%      \textheight      +
+%      \footnotesep     +
+%      \footheigth 
+%
+%  - 1 pt == 1/72 inch == 0.35 mm
+%
+% page width... left margin 4cm, right 2cm
+%
+\setlength{\hoffset}{0mm}
+\setlength{\marginparsep}{0mm}
+\setlength{\marginparwidth}{0mm}
+\setlength{\textwidth}{\paperwidth}
+\addtolength\textwidth{-60 mm}
+\setlength{\oddsidemargin}{-1 in}
+\addtolength\oddsidemargin{40 mm}
+
+% page height... textheight starts at ~4cm from top, 
+% i.e. 1 in + 12 pt + 24 pt = 1.5 in 
+%
+\setlength{\voffset}{0 mm}
+\setlength{\topmargin}{0 mm}
+\setlength{\headheight}{14.5 pt}
+\setlength{\headsep}{24 pt}
+\setlength{\textheight}{230mm}
+\setlength{\footnotesep}{12 pt}
+
+ 
+% setup linespacings to suit to the (stupid) requirement of double spacing
+%
+% - fake 1.5 linespacing
+%    \renewcommand{\baselinestretch}{1.241}
+%
+% - fake 2 linespacing
+%
+\renewcommand{\baselinestretch}{1.655}
+  
+%% Modify the \printbibliography command
+\defbibheading{bibliography}{
+  %\eofpages
+  % label here if we don't include references
+  \ifutuftthesis@includereferences
+  \else
+  \label{endofpages}
+  \fi
+  \chapter*{\refs}
+  \addcontentsline{toc}{chapter}{\refs}
+}
+
+\let\oldprintbibliography\printbibliography
+
+\renewcommand{\printbibliography}{
+  \oldprintbibliography
+  % label here if we also include references
+  \ifutuftthesis@includereferences
+    \label{endofpages}
+  \fi
+\langfin{
+  \selectlanguage{finnish}
+}{
+  \selectlanguage{english}
+}
+}
+
+%% Macro for hiding the chapter texts
+\ifutuftthesis@hidechapters
+  \renewcommand{\@chapapp}{\relax}
+  \renewcommand{\@makechapterhead}[1]{%
+    \vspace*{50\p@}%
+    {\parindent \z@ \raggedright \normalfont
+      \ifnum \c@secnumdepth >\m@ne
+          \huge\bfseries \@chapapp\space \thechapter\space\space
+      \fi
+      \interlinepenalty\@M
+      \Huge \bfseries #1\par\nobreak
+      \vskip 40\p@
+    }}
+\fi
+
+%% entry environment
+\newenvironment{entry}[1]{\begin{list}{}{%
+    \renewcommand{\makelabel}[1]{\textrm{##1:}\hfil}%
+    \settowidth{\labelwidth}{\textrm{#1:}}%
+    \setlength{\leftmargin}{\labelwidth}%
+    \addtolength{\leftmargin}{\labelsep}}}
+{\end{list}}
+
+
+%% the first abstract
+\langfin{
+  \renewenvironment{abstract}{\@bstracth{finnish}}{\@bstractt{finnish}{\k@ywords}}
+
+  %% the second abstract environment
+  %% always in english - but can be omitted if the document uses a single language
+  \newenvironment{abstracten}{\@bstracth{english}}{\@bstractt{english}{\k@ywordstwo}}
+}{
+  \renewenvironment{abstract}{\@bstracth{english}}{\@bstractt{english}{\k@ywords}}
+}
+
+%% title page, reuses the standard \maketitle
+\renewcommand*{\maketitle}{%
+  \clearpage 
+  \thispagestyle{empty}
+  \vspace*{\stretch{1}}
+  \vspace{3mm}
+  \noindent 
+  \rule{\linewidth}{1 mm}
+  \renewcommand{\baselinestretch}{1.05}\normalsize
+  \begin{flushright}
+  \Huge{\@title}
+  \end{flushright}
+  \rule{\linewidth}{1 mm}
+  \vspace*{\stretch{4}}
+
+  \renewcommand{\baselinestretch}{1}\normalsize
+  \begin{adjustbox}{minipage=65mm,right}
+  \thesistype{\l@nguage}\\
+  \university{\l@nguage}\\
+  \department{\l@nguage}\\
+  \labname{\l@nguage}\\
+  \ye@r\\
+  \@author
+  \ifdefstring{\s@pervisors}{none}{}{\\
+    \\
+    \supervisorstext:
+    \expandafter\forcsvlist\expandafter\\\expandafter{\s@pervisors}
+  }
+  \end{adjustbox}
+  \vspace*{\stretch{1}}\\
+  \scriptsize \turnitin
+  
+  \renewcommand{\baselinestretch}{1.655}\normalsize
+  \clearpage
+}
+
+%% used to detect the empty \totalpages on the first latex run
+\newcommand*{\IsInteger}[3]{\IfStrEq{#1}{ }{}{\IfInteger{#1}{#2}{#3}}}
+
+%% top part of the abstract page (since we use environments)
+\newcommand{\@bstracth}[1]{%
+  \selectlanguage{#1}
+  \setlength{\headheight}{0 pt}
+  \setlength{\headsep}{0 pt}
+  \addtolength\topmargin{-0.54 cm}
+  \renewcommand{\baselinestretch}{1}\normalsize
+  \clearpage
+  \thispagestyle{empty}
+  \noindent
+  \begin{minipage}[\textheight]{\linewidth}
+  \University{#1}\\
+  \department{#1}
+  \begin{entry}{}
+  \item[\textsc{\@author}] \@title
+  \end{entry}
+  \thesistype{#1},
+  \IsInteger{\totalpages}{
+    \pageref{endofpages} \pagesname{#1}%
+    \ifnum \totalpages > 0
+      , \totalpages\ \apppagesname{#1} \\
+    \else
+      \\
+    \fi
+  }{ \runagain{#1} \\
+  }
+  \labname{#1}\\
+  \titlecap{\monthname{#1}{\m@nth}} \ye@r\\
+  \rule{\linewidth}{1 pt}
+  \\
+}
+
+%% bottom part of the abstract page
+\newcommand{\@bstractt}[2]{%
+  \selectlanguage{#1}
+  \bigskip
+  \\
+  \begin{entry}{}
+    \item[\kwlabel{#1}] #2
+  \end{entry}
+  \end{minipage}
+  \clearpage
+  \setcounter{page}{0}
+  \setlength{\headheight}{12 pt}
+  \setlength{\headsep}{24 pt}
+  \setlength{\topmargin}{0 mm}
+  \renewcommand{\baselinestretch}{1.655}\normalsize
+  
+  % roman numbering for table of contents etc.
+  \pagenumbering{roman}
+}
+
+%% define \figcaption and \tblcaption for use with non-floating figures and tables
+\newcommand\figcaption{\def\@captype{figure}\caption}
+\newcommand\tblcaption{\def\@captype{table}\caption}
+
+%% compatible with old versions that required using \startpages
+%% now optional
+\newcommand{\startpages}{\initdocstyle}
+
+%% initializes the settings for main document content
+\newcommand{\initdocstyle}{%
+  \renewcommand{\startpages}{}
+\langfin{
+  \selectlanguage{finnish}
+}{
+  \selectlanguage{english}
+}
+  \clearpage
+  % clear 1st page headers/footers per request
+  \ifutuftthesis@emptyfirstpages
+  \fancypagestyle{plain}{\fancyhf{}\renewcommand{\headrulewidth}{0 pt}}
+  \fi
+  \pagenumbering{arabic}
+  \setcounter{page}{1}
+  \renewcommand{\baselinestretch}{1.655}
+  \pagestyle{fancy}
+  \renewcommand{\chaptermark}[1]{\markboth{\MakeUppercase{\chaptername\ \thechapter.\ ##1}}{}}
+  \lhead{\leftmark}
+  \chead{\version}
+  \rhead{\thepage}
+  \fancyfoot{}
+  % the totalcount counter is only used for appendices, set a bogus value now
+  % fails if the document has over 1000 pages
+  \setcounter{totalcount@page}{-1000}
+}       
+
+%% postpone the doc style through these sections
+\pretocmd{\tableofcontents}{\renewcommand{\startpages}{}}{}{}
+\apptocmd{\tableofcontents}{\renewcommand{\startpages}{\initdocstyle}}{}{}
+\pretocmd{\listoffigures}{\renewcommand{\startpages}{}}{}{}
+\apptocmd{\listoffigures}{\renewcommand{\startpages}{\initdocstyle}}{}{}
+\pretocmd{\listoftables}{\renewcommand{\startpages}{}}{}{}
+\apptocmd{\listoftables}{\renewcommand{\startpages}{\initdocstyle}}{}{}
+
+%% finally initialize the main document style
+\pretocmd{\chapter}{\startpages}{}{}
+
+%% compatible with old versions that required using \appendices
+%% now optional
+\newcommand{\appendices}{\startappendices}
+
+%% initializes the settings for appendices
+\newcommand{\startappendices}{%
+  \renewcommand{\appendices}{}
+  \clearpage
+  \appendix
+  \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}{0}
+}
+
+%% use in place of \chapter
+\newcommand{\appchapter}[1]{%
+  \appendices
+  \addtocounter{totalcount@page}{\value{page}}
+  \clearpage
+  \setcounter{page}{1}
+  \chapter{#1}
+}