@Paula, re: non-linear
If you open your epub in the calibre Editor, then open the .opf file (under Miscellaneous in the File Browser) you will probably see something similar to the following "matched pair" in the manifest and spine sections
Code:
<manifest>
...
<item href="text/myproblemfile.xhtml" id="someid" media-type="application/xhtml+xml"/>
...
</manifest>
<spine toc="toc">
...
<itemref idref="someid" linear="no"/>
...
</spine>
To stop the file with id "someid" being non-linear you can either change
linear="no" to
linear="yes" or delete
linear="no" entirely (it should then automatically default to "yes")
Non-linear files will display at the end of the book. When reading the epub, the rest of your text files will appear in the order listed within the <spine> section. So to fix your problem you will need to make sure you have your <spine> in the correct order
and your TOC in the matching order. Correcting it in TOC only is not enough.
ETA: In case you were wondering, the order your files are listed in the <manifest> section is not important.